简体   繁体   English

二进制表达式nsstring和id的无效操作数

[英]invalid operands to binary expression nsstring and id

hi i am getting the errer invalid operends to binary expression nsstring and id even i use typecasting this is the code in which i have problem . 您好,即使我使用类型转换,我也会得到错误的无效操作符,以二进制表达式nsstring和id表示,这是我遇到问题的代码。 kindly correct this code. 请更正此代码。

for (int j = 0 ; j<newarray.count ; j++){
        if(j<newarray.count){
 message = (NSString *) message + [newarray objectAtIndex:j]+ "," ;
        }
    }

You can not use + operator with objects, for your specific case you may replace the whole cycle with: 您不能对对象使用+运算符,对于您的特定情况,可以将整个循环替换为:

NSString* message = [newarray componentsJoinedByString:@","];

And FIY Objective-C does not support operator overloading at all. 而且FIY Objective-C根本不支持运算符重载。

I'm not really sure if this was your intention, but if you were trying to append new information to the string separated by commas you could go with something like this: 我不确定这是否是您的意图,但是如果您尝试将新信息附加到用逗号分隔的字符串中,则可以使用以下内容:

for (int j = 0 ; j<newarray.count ; j++){
    [message stringByAppendingString:[NSString stringWithFormat:@"%@,",[newarray objectAtIndex:j]]];
}

Additionally, your condition if(j<newarray.count) would always evaluate true in this loop, and is therefore unnecessary. 另外,您的条件if(j<newarray.count)在此循环中将始终为true,因此是不必要的。

You should cast "[newarray objectAtIndex:j]", whose default return type is "id" 您应该转换“ [newarray objectAtIndex:j]”,其默认返回类型为“ id”

Check this 检查一下

Also IIRC NSString doesn't have + operand overloaded. 另外,IIRC NSString没有+操作数重载。 So maybe you should try [NSString stringWithFormat:]. 因此,也许您应该尝试[NSString stringWithFormat:]。

You seem to be assuming NSString acts like std::string in C++. 您似乎假设NSString行为类似于C ++中的std::string Try this: 尝试这个:

NSMutableString *message = ...;
for (unsigned j = 0; j < newarray.count; j++)
{
    if (j > 0)
        [message appendString:@", "];
    [message appendString:[newarray objectAtIndex:j]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM