2012-07-06 63 views
2

我試圖在NSMutableArray中存儲一些數據。這是我的結構:NSMutableArray:添加和提取結構

typedef struct{ 
    int time; 
    char name[15]; 
}person; 

這是代碼添加人:

person h1; 
h1.time = 108000; 
strcpy(h1.name, "Anonymous"); 
[highscore insertObject:[NSValue value:&h1 withObjCType:@encode(person)] atIndex:0]; 

所以,我嘗試用這種方式來提取:

NSValue * value = [highscore objectAtIndex:0]; 
person p; 
[value getValue:&p]; 
NSLog(@"%d", p.time); 

的問題是,最終日誌不顯示我108000!
有什麼不對?

+0

是否有您所使用的交流結構,而什麼特別的原因比實際的對象?除非你這樣做,否則與物體一起去。 – Regexident 2012-07-06 16:07:42

+0

我不知道另一種方式來存儲兩種類型的數據(int和字符串)在同一個索引。我糾正了「角色」,對不起。 – charles 2012-07-06 16:12:16

+0

@ user1431646創建一個自定義類。 – 2012-07-06 16:14:00

回答

1

正如我最初的評論有規定很少有人用純粹的結構來做這種事情。相反,與真正的類對象去:

如果你不熟悉的語法下面你可能想看看在ObjC 2.0這些快速教程,以及閱讀蘋果文檔:

Person類:

// "Person.h": 
@interface Person : NSObject {} 

@property (readwrite, strong, nonatomic) NSString *name; 
@property (readwrite, assign, nonatomic) NSUInteger time; 

@end 

// "Person.m": 
@implementation Person 

@synthesize name = _name; // creates -(NSString *)name and -(void)setName:(NSString *)name 
@synthesize time = _time; // creates -(NSUInteger)time and -(void)setTime:(NSUInteger)time 

@end 

類用途:

#import "Person.h" 

//Store in highscore: 

Person *person = [[Person alloc] init]; 
person.time = 108000; // equivalent to: [person setTime:108000]; 
person.name = @"Anonymous"; // equivalent to: [person setName:@"Anonymous"]; 
[highscore insertObject:person atIndex:0]; 

//Retreive from highscore: 

Person *person = [highscore objectAtIndex:0]; // or in modern ObjC: highscore[0]; 
NSLog(@"%@: %lu", person.name, person.time); 
// Result: "Anonymous: 108000" 

爲了簡化調試您可能還需要Person實現description方法:

- (NSString *)description { 
    return [NSString stringWithFormat:@"<%@ %p name:\"%@\" time:%lu>", [self class], self, self.name, self.time]; 
} 

,這將使你只是這樣做了記錄:

NSLog(@"%@", person); 
// Result: "<Person 0x123456789 name:"Anonymous" time:108000> 
+0

謝謝大家,我糾正了所有的問題,但是我犯的真正錯誤是我忘記初始化陣列! – charles 2012-07-06 16:41:40

+0

@ user1431646:仍然,與對象一起去。沒有理由不這樣做。但數百人贊成。方便的名稱字符串處理和內存分配只是爲了開始。 – Regexident 2012-07-06 16:55:35

0

重新實現人作爲一個Objective-C的對象,並從中獲益:

Person.h:

@interface Person : NSObject 
{ 
    int _time; 
    NSString *_name; 
} 

@property (assign, nonatomic) int time; 
@property (retain, nonatomic) NSString *name; 

@end 

Person.m:

#import "Person.h" 

@interface Person 
@synthesize time = _time; 
@synthesize name = _name; 

- (id)init 
{ 
    self = [super init]; 
    if (self != nil) 
    { 
     // Add init here 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    self.name = nil; 
    [super dealloc]; 
} 

@end 
+1

無需在接口中聲明私有成員,'@ property'和'@ synthesize'就足夠了。而且,在ARC中,dealloc(和init)都是不必要的。 4.5甚至可以刪除'@ synthesize',只留下屬性聲明。 – 2012-07-06 16:26:49

+0

@HampusNilsson這是我喜歡使用的風格,這個實現應該適用於幾乎任何編譯器和內存環境。 – trojanfoe 2012-07-06 16:38:31

2

您的代碼看起來正確(並適用於我),所以我推斷你沒有初始化highscore。所以當你發送insertObject:atIndex:消息給它時,沒有任何反應。當你發送objectAtIndex:方法時,你會得到零回。當您發送getValue:到零NSValue *value,它什麼都不做,所以你person p是左充滿隨意堆的垃圾,這就是爲什麼你的NSLog不打印108000.