Я получаю Property 'aVariable' not found on object of type id
при попытке чтения или записи aVariable в массив. Разве не должно быть известно, какой класс является объектом, который я добавил? Также заметил, что он работает, чтобы прочитать значение с помощью NSLog(@" %@",[[anArray objectAtIndex:0] aVariable]);
Я новичок в Objective C, поэтому это может быть какая-то простая вещь, которую я не получаю.
AnObject
@interface AnObject : NSObject
@property (nonatomic,readwrite) int aVariable;
@end
AnotherObject
@interface AnotherObject : NSObject
@end
test.h
#import "test.h"
@implementation AnObject
@synthesize aVariable;
- (id)init
{
self = [super init];
if (self) {
aVariable=0;
}
return self;
}
@end
test.m
@implementation AnotherObject
- (id)init
{
self = [super init];
if (self) { }
return self;
}
- (NSMutableArray*) addToArray
{
NSMutableArray* anArray = [[NSMutableArray alloc] initWithCapacity:0];
AnObject* tempObject = [[AnObject alloc] init];
tempObject.aVariable=10;
[anArray addObject:tempObject];
// Property 'aVariable' not found on object of type 'id'
[anArray objectAtIndex:0].aVariable=[anArray objectAtIndex:0].aVariable + 1;
// Property 'aVariable' not found on object of type 'id'
NSLog(@" %i",[anArray objectAtIndex:0].aVariable);
// This works
NSLog(@" %i",[[anArray objectAtIndex:0] aVariable]);
return anArray;
}
@end