簡體   English   中英

為什么不能使用對象初始化此NSArray?

[英]Why can I not initialize this NSArray With Objects?

我有一個包含定制類對象的數組。 但是,在初始化時,編譯器給我一個錯誤- "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文字帶有方括號,

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的,但是我想這只是一個示例片段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM