简体   繁体   中英

Objc to swift bridge `use of undeclared identifier 'cocoarr'`

I'm combining Swift and Objective-C in the same project. I am trying to use STTwitter cocoapod like this:

// 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 tried not investigate variable passed to successBlock , they all have valid values). But in Swift, when successBlock gets executed, tweets was:

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 NSArray into Swift? (No compile error)

That worked for me.

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

Try to specify the type of the array instead AnyObject.

I hope it helps.

Cheers.

Try [[String:AnyObject]] rather than [AnyObject]

I got the same error in a smilar functionality. When changing from:

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
}

I got it working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM