简体   繁体   English

NSInteger到NSString误解

[英]NSInteger to NSString mis understood

I have a question regarding NSInteger to NSString i keep trying but with no luck. 我有一个关于NSIntegerNSString的问题,我一直在尝试,但是没有运气。 however for some strange reason one of my NSInteger does but i dont know why i have this method: 但是由于某种奇怪的原因,我的NSInteger确实做到了,但是我不知道为什么我有这种方法:

//Lets say loc_id = 869
-(void)addResource:(NSInteger)loc_id
{
   //[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
   NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);
}

Results BAD 结果差

Because its giving me: addResource: loc_id:20987787696 in the debugger of xcode 因为它给了我:xcode调试器中的addResource:loc_id:20987787696

Its ment to be loc_id:860 它的位置为loc_id:860

when i try this with loc_id = 8 it works pretty well why is this strange behaviour or what did i mis understood thanks for the tips and help. 当我尝试使用loc_id = 8 ,效果很好,为什么会出现这种奇怪的行为或我误解了什么呢?感谢您的提示和帮助。 when i use %@ in string format it works for loc_id = 8 but when i try to use %d wich is normal useage it wil also result in loc_id:20987787696 how come ? 当我以字符串格式使用%@ ,它适用于loc_id = 8但是当我尝试正常使用%d它也会导致loc_id:20987787696怎么回事?

Useage of method: 方法的用途:

-(IBAction)switchChanged:(id)sender {
  UISwitch *switchControl = sender;
  [loc addResource:switchControl.tag] 
 }

 -(void)addSwitch:(NSInteger)aid
 {
    UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(x,y,w,h)]];
    sw.tag = aid;
    [sw addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEvenValueChanged]];
    [self.View addSubview:sw];
 }

 //Data is dynamic (NSObject) but for example i will exclude the dynamic data.
 for(int i = 0; i < 20; i++)
 {
   NSInteger *loc_id = 868;
   [self addSwitch:loc_id];

 }

at a point i want to insert it into the NSMutableArray as commented out above but i expect a value of 869 and it giefs me 2071894069 i want the value of the int not the memory thingy 在某一点上我想将其插入如上所述的NSMutableArray中,但是我希望它的值为869,这让我感到遗憾2071894069我想要int的值而不是内存问题

Kind Regards, Stefan 亲切的问候,斯特凡

You should use %li or @ld instead of %@ , and drop the stringWithFormat: call in the NSLog (it already expects a format): 您应该使用%li@ld而不是%@ ,并在NSLog放置stringWithFormat:调用(它已经期望使用格式):

NSLog(@"addResource: loc_id:%li",loc_id);

%@ is the selector to format objects descriptions and strings. %@是用于格式化对象描述和字符串的选择器。 %li is to cast your integer to a long, and prevent an eventual compiler warning. %li是将整数强制转换为长整数,并防止最终出现编译器警告。


Edit : 编辑:

After quite a few questions in the comment, here is another wrong thing in your code : 在注释中提出了很多问题之后,这是代码中的另一错误:

NSInteger *loc_id = 868;

You should not initiate loc_id as a pointer. 您不应将loc_id初始化为指针。 Use instead : 改为使用:

NSInteger loc_id = 868;

Edit #2 : 编辑#2:

As discussed in the chat, the issue came from the way the NSInteger value was extracted from the array / dictionary in which it was held. 正如聊天中所讨论的那样,问题出在从NSInteger值从保存它的数组/字典中提取的方式。

To display your NSInteger, you don't need to format it in NSString. 要显示您的NSInteger,您无需使用NSString对其进行格式化。 Just use this code below. 只需在下面使用此代码。

NSLog(@" %ld",(long)loc_id);

You need to use %ld or %li for integers. 您需要对整数使用%ld或%li。 %@ is used for objects. %@用于对象。

NSLog([NSString stringWithFormat:@"addResource:loc_id:%ld",(long)loc_id]);

You also can cast your Integer to a long value: 您还可以将Integer转换为long值:

-(void)addResource:(NSInteger)loc_id
{
    NSLog(@"addResource: loc_id:%ld",(long)loc_id);
}

Apple Documentation 苹果资料

The utility functions NSLog() and NSLogv() use the NSString string formatting services to log error messages. 实用程序函数NSLog()NSLogv()使用NSString字符串格式化服务来记录错误消息。 Note that as a consequence of this, you should take care when specifying the argument for these functions. 请注意,因此,在为这些函数指定参数时应格外小心。 A common mistake is to specify a string that includes formatting characters, as shown in the following example. 一个常见的错误是指定一个包含格式化字符的字符串,如以下示例所示。

Wrong 错误

You don't need to add formatting strings inside NSLog: 您无需在NSLog中添加格式字符串:

NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);

Right

Also using %li on 64 Bit gives integer precision as @rdurand asnwer 64位上还使用%li将整数精度设置为@rdurand asnwer

NSLog(@"addResource: loc_id:%li",loc_id);
    NSLog([NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id]);

Gets a warning: Format string is not a string literal. 收到警告:格式字符串不是字符串文字。

The following code works as ordered: 以下代码按顺序工作:

-(void)addResource:(NSInteger)loc_id
{
    //[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
    NSString *string = [NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id];
    NSLog(@"%@", string);
}

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

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