이 내 최초의 코멘트에 명시된 바와 같이이 순수한 구조체로 이런 종류의 작업을하는 이유는 거의 없습니다. 대신 실제 클래스 객체로 이동합니다
애플의 문서 ObjC 2.0이 빠른 튜토리얼을 볼뿐만 아니라 읽을 수 있습니다 다음은 구문에 익숙하지 않은 경우
:
사람 클래스 :
// "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
,536,913 63,210
클래스 사용 :
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %p name:\"%@\" time:%lu>", [self class], self, self.name, self.time];
}
그냥 로깅이 작업을 수행 할 수 있습니다 :
#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"
당신은 또한 description
방법을 구현하는 Person
을 할 수 있습니다 디버깅을 간소화하기 위해
NSLog(@"%@", person);
// Result: "<Person 0x123456789 name:"Anonymous" time:108000>
를 인쇄하지 않는 이유는 임의 스택 쓰레기로 가득 왼쪽되는 것은 당신이 아니라 교류 구조체를 사용하는 특별한 이유가
person p
당신, 아무것도하지 않는 실제 물건보다? 그렇지 않으면 물건을 가지고 가십시오. – Regexident두 가지 유형의 데이터 (int 및 string)를 동일한 인덱스에 저장하는 다른 방법을 모른다. 나는 "페르소나"를 정정했다, 미안. – charles
@ user1431646 사용자 지정 클래스를 만듭니다. –