正如我最初的評論有規定很少有人用純粹的結構來做這種事情。相反,與真正的類對象去:
如果你不熟悉的語法下面你可能想看看在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>
是否有您所使用的交流結構,而什麼特別的原因比實際的對象?除非你這樣做,否則與物體一起去。 – Regexident 2012-07-06 16:07:42
我不知道另一種方式來存儲兩種類型的數據(int和字符串)在同一個索引。我糾正了「角色」,對不起。 – charles 2012-07-06 16:12:16
@ user1431646創建一個自定義類。 – 2012-07-06 16:14:00