简体   繁体   中英

Adding a dictionary to NSMutableArray from swift

I have an Obj-c superclass, with

@property (strong, nonatomic) NSMutableArray *sectionChanges;

And in Swift I am trying to add a dictionary to it:

self.sectionChanges.addObject([NSFetchedResultsChangeType.Insert: 1])

We get

'AnyObject' does not have a member named 'Key'

I have tried a lot of options:

self.sectionChanges.addObject([NSFetchedResultsChangeType.Insert: 1] as NSDictionary)

Changed the error to

Type 'NSFetchedResultsChangeType' does not conform to protocol 'NSCopying'

Then I try:

self.sectionChanges.addObject([Int(NSFetchedResultsChangeType.Insert): 1] as NSDictionary)

and get:

Cannot invoke 'init' with an argument of type 'NSDictionary'

Running out of options... Then same code in Obj-c is simple:

[self.sectionChanges addObject:@{@(type): @(sectionIndex)}];

“Swift imports as a Swift enumeration any C-style enumeration marked with the NS_ENUM macro.”

Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/tw/1u3-0.l

So NSFetchedResultsChangeType is a Swift enumeration now, and you can't pass it to Objetive-C.

You might want to try this:

self.sectionChanges.addObject([NSFetchedResultsChangeType.Insert.rawValue: 1])

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