简体   繁体   English

客观C字符串混淆

[英]objective C string Confusion

I am having code 我有代码

NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];

when i try to print NSLog(@"cell value is %@",cellValue1); 当我尝试打印NSLog(@"cell value is %@",cellValue1);

in log i am not getting anything, if i use %s, i am getting some symbols, not the string located in cellValue1. 在日志中,我什么也没得到,如果我使用%s,我得到的是一些符号,而不是位于cellValue1中的字符串。

Please help me. 请帮我。

Thanks in advance. 提前致谢。

Check to make sure that products1 is actually set. 检查以确保products1实际上是设置。 It sounds as though it's nil when you send it the -objectAtIndex: message. 发送-objectAtIndex:消息时,听起来好像为零。

这肯定意味着您的字符串为空...用length方法检查它...

Solution: 解:

NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];
NSLog([NSString stringWithFormat:@"Cell Value is %@", cellValue1]);

Reason: NSLog operates with String inputs. 原因: NSLog使用字符串输入进行操作。 While your statement should work, if there is some/any issue with your original cellValue1 string, your original statement will not catch the issue and assure that that NSLog() is being handed a pure string. 尽管您的语句应该可以工作,但是如果原始cellValue1字符串存在某些问题,则原始语句将无法捕获问题,并确保将NSLog()传递给纯字符串。 By using the stringWithFormat: syntax you assure that even if your cellValue1 values is null or nil, you will receive your "cell value is" comment and possible some hint as to what is being passed into the statement by your cellValue1 string. 通过使用stringWithFormat:语法,可以确保即使cellValue1值为null或nil,您也将收到“单元格值为is”注释,并可能获得有关cellValue1字符串传递给语句的内容的一些提示。

Testing Note: If the above doesn't work for you, Test your original string by just using NSLog(cellValue1); 测试注意:如果上述方法对您不起作用,请仅使用NSLog(cellValue1);来测试原始字符串NSLog(cellValue1); . If this doesn't work it will tell you that your original NSString is not properly pulling your product at indexPath.row values. 如果这不起作用,它将告诉您原始的NSString没有在indexPath.row值处正确提取您的产品。

Hope this helps! 希望这可以帮助!

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

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