1

I am working on an app where I am getting an API response and need to create objects from this and put it in an Array to use later. The API call is made in Objective-C, but my new Object is made in Swift (it's an old app with mostly Objective-C code).

The API call itself is working fine, but when I try to convert this the object an object is not made.

This is the object code (Swift):

import Foundation

@objc
class GCBOKSProductInfo: NSObject, NSCoding {

var featureName: String = ""
var available: Bool = false

static let sharedInstance = GCBOKSProductInfo()

override init() {
    super.init()
}

init(featureName: String, available: Bool) {
    super.init()

    self.featureName = featureName
    self.available = available
}

convenience required init?(coder decoder: NSCoder) {
    guard let featureName = decoder.decodeObject(forKey: "featureName") as? String
        else { return nil }

    self.init(
        featureName: featureName,
        available: decoder.decodeBool(forKey: "available")
    )
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(featureName, forKey: "featureName")
    aCoder.encode(available, forKey: "available")
}

}

and here is where I am trying to make an object out of the API response:

NSMutableArray *result = [NSMutableArray arrayWithCapacity:[responseObject count]];

for (NSDictionary *boksProductInfoDict in responseObject) {

    NSString *featureName = boksProductInfoDict[@"FeatureNaam"];
    NSString *available = boksProductInfoDict[@"Beschikbaar"];

    GCBOKSProductInfo *boksProductInfo = [[GCBOKSProductInfo alloc] initWithFeatureName:featureName available:available.boolValue];

    [result addObject:boksProductInfo];
}

The extracting of the featureName and available works (if I put a breakpoint in the object I see that it is made correctly), but the "boksProductInfo" never seems to be allocated/initialized correctly, so when I add it to the "result" array there is no information in the entry.

If I try to po the "boksProductInfo" I get this:

ProjectName.GCBOKSProductInfo: 0x60800005ee70

And if I try to po "boksProductInfo.featureName" I get this:

error: member reference type 'GCBOKSProductInfo *' is a pointer; did you mean to use '->'?

error: incomplete definition of type 'GCBOKSProductInfo' forward declaration of 'GCBOKSProductInfo'

Any idea where I might have it wrong?

Umang Loriya
  • 840
  • 8
  • 15
Jay
  • 31
  • 5
  • You have to use the bridging header. Refer this link [click here](https://stackoverflow.com/questions/24206732/cant-use-swift-classes-inside-objective-c) – divya d Sep 19 '17 at 12:52
  • I have already included this in the Objective-C file, but it is still not working correctly. I did a work-around that works now, sending the entire NSDictionary in the array instead in the result and then unwrapping it where I need it. Not elegant, but working for now. – Jay Sep 19 '17 at 13:49

0 Answers0