【问题标题】:How do I increment an int value in an NSMutableArray?如何增加 NSMutableArray 中的 int 值?
【发布时间】: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


【解决方案1】:

最简单的方法是访问属性counter,但要做到这一点,你需要转换objectAtIndex:的结果,因为它返回一个id,而Objective-C允许你调用任何方法id 实例,您不能调用任何属性:

((THEOBJECT *)[logins objectAtIndex:index_counter]).counter++;

【讨论】:

    【解决方案2】:

    这一行中的counter 是一个访问器方法:

    [[logins objectAtIndex:index_counter] counter]
    

    它返回一个值给你,所以它不是你可以设置的(就像说50 = 100——你不能设置这样的值)。

    如果要设置变量,需要使用setCounter方法:

    [[logins objectAtIndex:index_counter] setCounter:[[logins objectAtIndex:index_counter] counter]+1];
    

    【讨论】:

      【解决方案3】:

      行内:

      [[logins objectAtIndex:index_counter] counter]=[[logins objectAtIndex:index_counter] counter]+1; //ERROR HERE.
      

      您应该使用赋值左侧属性counter 的setter 而不是getter。所以你应该把那行改成:

      [[logins objectAtIndex:index_counter] setCounter:[[logins objectAtIndex:index_counter] counter]+1];
      

      为了更清楚,您可以将该行分成两行:

      int currentValue = [[logins objectAtIndex:index_counter] counter];
      [[logins objectAtIndex:index_counter] setCounter:currentValue+1];
      

      您也可以使用点符号并将其写为:

      THEOBJECT *myObject = [logins objectAtIndex:index_counter];
      int currentValue = myObject.counter;
      myObject.counter = currentValue + 1;
      

      或者:

      THEOBJECT *myObject = [logins objectAtIndex:index_counter];
      myObject.counter = myObject.counter + 1;
      

      或者:

      THEOBJECT *myObject = [logins objectAtIndex:index_counter];
      myObject.counter++;
      

      【讨论】:

        猜你喜欢
        • 2016-07-02
        • 1970-01-01
        • 2019-06-25
        • 2021-11-11
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 2011-04-18
        • 1970-01-01
        相关资源
        最近更新 更多