NSArray
文档是你的朋友。
无论如何,要访问对象数组,您可以使用- (id)objectAtIndex:(NSUInteger)index
方法NSArray
。
什么塞瓦阿列克谢耶夫是说你的班级必须以不同的方式组织。例如:
//.h
@interface MyCustomObject : NSObject
{
// you dont need to declare variables, the compiler will do it for you
}
@property (nonatomic, copy) NSString* objObservation;
@property (nonatomic, assign) NSInteger objId;
@property (nonatomic, copy) NSString* objDate;
@property (nonatomic, copy) NSString* objDevice;
@property (nonatomic, assign) double objLatitude;
@property (nonatomic, assign) double objLongitude;
@end
//.m
@implementation MyCustomObject
@synthesize objId;
@synthesize objDate;
@synthesize objDevice;
@synthesize objLatitude;
@synthesize objLongitude;
@synthesize objObservation;
- (void)dealloc
{
/* uncomment if you don't use ARC
[objDate release];
[objDevice release];
[objObservation release];
[super dealloc];
*/
}
@end
使用你的类像这样(你需要#import "MyCustomObject.h"
)
// populate your object
MyCustomObject* myObj = [[MyCustomObject alloc] init];
myObj.objDate = @"yourDate";
myObj.objDevice = @"yourDevice";
myObj.objId = 1;
myObj.objLatitude = 22.0;
myObj.objLongitude = 23.87;
myObj.objObservation = @"yourObservation";
// insert it into your array (here only for test purposes but you could create an instance variable for store it)
NSArray* myArray = [NSArray arrayWithObjects:myObj, nil];
// if you don't use ARC
[myObj release];
// grab the object from the array
MyCustomObject * currentObj = [myArray objectAtIndex:0];
// see its values, for example
NSLog(@"%@", [currentObj objDevice]);
现在我想补充一些注释。
- 当你使用class时,用大写字母(例如,
MyClass
而不是myclass
,则变量以小写开始,例如, myVariable
- 如果你的阵列可以在不同的时间填充,你需要一个
NSMutableArray
。 A NSArray
一经创建就无法更改。 NSMutableArray
反而是动态的,你可以添加或删除对象
- 当你处理对象时,你应该用属性包装你的变量(在这种情况下,你不需要变量,因为编译器会为它们提供
@synthesize
指令)。这样,您就不能打破的对象封装
- 你能想到的,如果你想
当你写问题,尽量把一些背景绕过它,试图解释你做了什么,以提供一个初始化到对象和你想实现什么。
你是什么意思“处理属性到数组”?你想插入一个属性到数组中吗?或者检索数组中的对象的属性?顺便说一句,你需要在这些变量周围创建一些属性,否则你将无法从'object'类外部访问它们。 – 2012-07-06 15:08:09
检索数组对象的属性。当然,我已经创建了属性,这只是一个伪代码示例 – user1507121 2012-07-06 15:10:06
要理解你所问的是什么非常困难,你能提供一个你想要做的事情类型的例子吗? – 2012-07-06 15:13:36