Objc to fast bridge `use undeclared identifier 'cocoarr``

I am combining Swift and Objective-C in the same project. I am trying to use STTwitter cocoapod as follows:

// objective-c
// STTwitter category method
//
- (void)getStatusesLookupTweetIDs:(NSArray *)tweetIDs 
                     successBlock:(void (^)(NSArray *))successBlock 
                       errorBlock:(void (^)(NSError *))errorBlock {

    [self getStatusesLookupTweetIDs:tweetIDs
                    includeEntities:@(YES)
                           trimUser:@(YES)
                                map:@(YES)
                       successBlock:successBlock
                         errorBlock:errorBlock];
}

Swift code

// swift
twitterApi.getStatusesLookupTweetIDs(ids, successBlock: { (tweets: [AnyObject]!) -> Void in
    process(tweets)
    finish()
}, errorBlock: { (err) -> Void in
    error(err)
})

Everything looks fine in Obj-C (I did not try to examine the variable passed in successBlock, they all have valid values). But in Swift, when successBlockexecuted, tweetswas:

Printing description of tweets:
([AnyObject]!) tweets = 1 value {
  [0] = <error: use of undeclared identifier 'cocoarr'
error: 1 errors parsing expression
>

}

How do I fix this and pass NSArrayto Swift? (No compilation error)

+4
source share
2 answers

It worked for me.

Instead of using:
[AnyObject]
try using:
[Dictionary<String, AnyObject>] (or whatever class is inside the array)

AnyObject.

, .

.

0

[[String:AnyObject]], [AnyObject]

. :

if let dictOrList = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &err) as? NSDictionary {
    callbackList = [dictOrList]
} else if let list = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &err) as? [AnyObject] {
    callbackList = list
}

to

if let dictOrList = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &err) as? [String: AnyObject] {
    callbackList = [dictOrList]
} else if let list = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &err) as? [[String:AnyObject]] {
    callbackList = list
}

.

0

All Articles