gpt4 book ai didi

ios - 从 swift 向 NSMutableArray 添加字典

转载 作者:行者123 更新时间:2023-11-29 02:31:38 25 4
gpt4 key购买 nike

我有一个 Obj-c 父类(super class),有

@property (strong, nonatomic) NSMutableArray *sectionChanges;

在 Swift 中,我试图向它添加一个字典:

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

我们得到

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

我尝试了很多选择:

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

错误改为

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

然后我尝试:

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

并得到:

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

用完选项...然后 Obj-c 中的相同代码很简单:

[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

所以 NSFetchedResultsChangeType 现在是一个 Swift 枚举,你不能将它传递给 Objetive-C。

您可能想尝试一下:

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

关于ios - 从 swift 向 NSMutableArray 添加字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com