我有一个包含定制类对象的数组。但是,在初始化时,编译器给我一个错误-"Lexical or Preprocessor" Expected ':'

interface myClass : NSObject
@property (readwrite, strong) NSString* name;
@property (readwrite, strong) NSString* home;
@property (readwrite, strong) Preference pref; // This is another custom class
-(id) initWithName:(NSString*) name home:(NSString*) home preference:(Preference) preference;
end

@interface MyViewController()
@property (nonatomic, strong) NSArray *rowArray;
@end

@implementation MyViewController
...
...
...

- (void) initializeArray
{
    self.rowArray = @{
                      [[myClass alloc] initWithName:@"Harry" home:@"New York" preference :Acura],
                      [[myClass alloc] initWithName:@"Win" home:@"Seattle" preference :Toyota];
                    };
}

有人可以告诉我我在搞什么,为什么会收到这个错误?

最佳答案

数组的Objective-C literal带有方括号,
NSArray *anArray = @[obj1, obj2]

在您发布的代码中,它试图制作字典,
NSDictionary *aDict = @{"key1" : obj1, @"key2" : obj2}
所以这就是为什么它说期望:的原因。

该行应显示为

self.rowArray = @[
    [[myClass alloc] initWithName:@"Harry" home:"New York" preference :Acura],
    [[myClass alloc] initWithName:@"Win" home:"Seattle" preference :Toyota];
];

正如其他人指出的那样,该代码还有其他一些错误,并且这些城市名称不是NSString的,但是我想这只是一个示例片段。

关于ios - 为什么不能使用对象初始化此NSArray?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22495664/

10-11 06:17
查看更多