【问题标题】:ios assigning value to a string from an arrayios将值分配给数组中的字符串
【发布时间】:2013-06-09 19:35:53
【问题描述】:

所以我有一个基本数组:

NSMutableArray *answerButtonsArrayWithURL = [NSMutableArray arrayWithObjects:self.playView.coverURL1, self.playView.coverURL2, self.playView.coverURL3, self.playView.coverURL4, nil];

里面的对象是字符串。我想从该数组中访问一个随机对象

int rndValueForURLS = arc4random() % 3;

并为其赋值。我尝试了许多不同的方法,但我最近的方法是

[[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS] stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]]; 

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: ios nsstring nsarray


    【解决方案1】:

    您需要分配它。你已经在建立这样的新价值了:

    NSString *oldValue = answerButtonsArrayWithURL[rndValueForURLS];
    NSString *newValue = [oldValue stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
    

    你缺少的部分:

    answerButtonsArrayWithURL[rndValueForURLS] = newValue;
    

    上面是用另一个替换 immutable 字符串的方法。如果字符串是可变的,也就是说,它们被创建为NSMutableString,你可以这样做:

    NSMutableString *value = answerButtonsArrayWithURL[rndValueForURLS];
    [value appendString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
    

    注意

    我在任何地方都替换了符号:

    [answerButtonsArrayWithURL objectAtIndex:rndValueForURLS];
    

    使用新的等价物和 IMO 更具可读性:

    answerButtonsArrayWithURL[rndValueForURLS];
    

    【讨论】:

    • 非常感谢。第一个解决方案不起作用,但让它们变得可变,女巫我应该首先做的,工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2011-10-23
    相关资源
    最近更新 更多