【发布时间】:2012-03-19 12:04:46
【问题描述】:
我明白了:
@interface ViewController : UIViewController
{
int index_counter;
//NSMutableArray *logins;
}
@property (weak, nonatomic) IBOutlet UITextField *count;
@property (strong, nonatomic) NSMutableArray *logins;
- (IBAction)next_button:(id)sender;
@end
这是一个存放对象的数组:
@interface THEOBJECT : NSObject
{
NSString *uname;
int counter;
}
-(void) SetUser: (NSString *) username;
-(void) SetCount: (int) value;
-(void) print;
@property (nonatomic, retain) NSString *uname;
@property (nonatomic,readwrite) int counter; //not sure if this is correct
@end
@implementation SiteValue
@synthesize uname;
@synthesize counter;
-(void) SetCount:(int) value
{
counter=counter+1;
}
@end
并且我的方法应该在数组的每个索引中增加对象 THEOBJECT 中的计数值:
- (IBAction)next_button:(id)sender
{
index_counter=index_counter-1;
if (index_counter<0)
{
index_counter=0;
}
username.text=[[logins objectAtIndex:index_counter] uname];
[[logins objectAtIndex:index_counter] counter]=[[logins objectAtIndex:index_counter] counter]+1; //ERROR HERE.
}
在我写“ERROR HERE”的地方,每次我按下 next 按钮并将 +1 存储在数组中时,它应该增加计数值。 但它给了我一个只读错误。确切的错误是"assigning to 'readonly' return result of an Objective-C message not allowed"。我认为最好的办法是调用setcount: 方法,但它不允许我调用它,因为它是两个不同的接口。有什么想法吗?
【问题讨论】:
-
我不知道你想在这里做什么。但它看起来非常复杂。尝试 setCounter:新的计数器值。当您始终可以从 NSMutableArray 获取索引和计数时,不确定为什么要在模型对象中保留索引。
标签: iphone objective-c xcode ios5 nsmutablearray