Swift- Waiting for asynchronous for-in loop to complete before calling completion handler swift

I'm baffled as to what is the best way to achieve this. I am trying to keep a running total of Double values that I am looping through and adding together, via a network call. Everything I've read says to use DispatchGroup. My completion either calls too early or doesn't get called at all and I've tried every configuration of .enter, .leave, and .wait that I can think of.

I'm baffled as to what is the best way to achieve this. I am trying to keep a running total of Double values that I am looping through and adding together, via a network call. Everything I've read says to use DispatchGroup. My completion either calls too early or doesn't get called at all and I've tried every configuration of .enter, .leave, and .wait that I can think of.

    let group = DispatchGroup()
    var runningTotal: Double = 0.00
ref.observeSingleEvent(of: .value) { (snapshot) in
    guard let bills = snapshot.value as? [String: AnyObject] else {
        //error
        return
    }

    for billId in bills.keys {
        group.enter()
        print("Entering")
        Database.database().reference().child("bills").child(billId).observeSingleEvent(of: .value, with: { (snapshot) in
            guard let bill = snapshot.value as? [String: AnyObject] else {
                return
            }
            if let amount = bill["amount"] as? Double {
                runningTotal += amount
            }
            group.leave()
            print("Leaving")
        })
    }
    completion(runningTotal)
}
group.wait()

}


Accessing Swift protocol property implemented in an Obj-C class from another Swift class

I've seen lots of questions regarding implementing Obj-C protocols in Swift, but not so much the other way around, and I haven't seen this specifically.

I've seen lots of questions regarding implementing Obj-C protocols in Swift, but not so much the other way around, and I haven't seen this specifically.

I am using a mixed Obj-C / Swift codebase. I have a Swift protocol defined as follows:

NamedItem.swift

@objc protocol NamedItem {
    var name: String { get }
}

I have an existing Objective-C class that currently has its own name property:

MyObjcClass.h

@interface MyObjcClass : NSObject
@property (nonatomic, strong, readonly) NSString* name;
@end

I have a couple other classes that have a name property, so obviously I'd like to associate them all with a protocol instead of typecasting to a bunch of different types. Now, if I try to switch my Obj-C class from having its own property to implementing the Swift protocol:

MyObjcClass.h

@protocol MyObjcProtocol
@property (nonatomic, strong, readonly) NSString* place;
@end

@interface MyObjcClass : NSObject
@end

MyObjcClass.m

@interface MyObjcClass () <NamedItem>
@end

@implementation MyObjcClass
@synthesize name = _name;
@synthesize place = _place;
@end

This works great, in my other Objective-C classes, but if I try to access the name property from a Swift class:

SomeSwiftClass.swift

let myObj = MyObjcClass()
myObj.name // error
myObj.place // no problem

I get the following error:

Value of type 'MyObjcClass' has no member 'name'

If I don't remove the existing @property declaration from MyObjcClass.h and omit the @synthesizestatement everything builds correctly. Which seems weird and wrong - If you adopt a Objc-C protocol from an Obj-C class you don't have to re-define the property, just the @synthesize statement is sufficient.

I've tried defining the Swift protocol about every way I could think of and have been able to find suggestions about, but I haven't been able to get around this.

So, what is the correct way to implement a Swift protocol property (maybe specifically a read-only property?) in an Objective-C class such that another Swift class can access it? Do I really have to re-declare the property in the Obj-C header? I know I could always give it up and just define the protocol in Objective-C, but... Swift is the future! (or something like that...)

Swift Programming Tutorial for Beginners

Swift Programming Tutorial for Beginners

Swift Programming Tutorial for Beginners: Learn how to code with Swift programming in one single tutorial!

Swift Programming Tutorial for Beginners

We'll go through all the core concepts of Swift including: Variables and Constants, Data Types, Properties, Functions and Parameters, Classes, IF Statements And a lot more!