2014-05-06 74 views
0

我有以下數據結構。我想將一個新對象添加到將存儲我的標記的新數組中。枚舉通過數據結構objective-c

({ title = "1"; 
    location = "2"; 
}, 
{ 
    title = "3"; 
    location = "4"; 
}) 

如何設置每個標記,以便新的數組中的第一對象具有爲1的值和與marker.location 2的值的屬性marker.title?

回答

0

您可以爲此創建一個新類,並將該類的對象添加到該數組。

@interface MyClass : NSObject 

@property (strong, nonatomic) NSString* title; 
@property (strong, nonatomic) NSString *location; 

@end 

我已經將類型設置爲NSString,但您可以將其更改爲您的首選項。

您可以創建和對象添加到數組如下:

MyClass *newObject = [[MyClass alloc]init]; 

newObject.title = @"1"; 
newObject.location = @"2"; 

//Adding to array 
[arr addObject:newObject]; 

有關讀取,可以實現:

for (MyClass *obj in arr) 
{ 

    NSLog (@"title: %@, location %@", obj.title, obj.location); 

} 
0

如果你想列舉「快速列舉」比快得多for循環:

[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    YOUR_OBJECT *current = obj 
    ......  

}];