English 中文(简体)
NSString IntValue 变形实际数字
原标题:NSString intValue deforming actual number

I was making a basic method that takes a Flickr image URL and returns the image s ID. I m passing the method the NSString @"http://farm6.staticflickr.com/5183/5629026092_c6762a118f".

目标是返回int: 5629026092 ,它是图像 s URL中的,是图像 s ID。

这是我的方法:

-(int)getImageIDFromFlickrURL:(NSString *)imageURL{
    NSArray *objectsInURLArray = [imageURL componentsSeparatedByString:@"/"];
    NSString *lastObjectInFlickrArray = [objectsInURLArray lastObject];
    NSArray *dirtyFlickrIdArray = [lastObjectInFlickrArray componentsSeparatedByString:@"_"];
    NSString *flickIDString = [dirtyFlickrIdArray objectAtIndex:0];
    NSLog(@"flickr id string: %@",flickIDString);
    int flickrID = [flickIDString intValue];
    NSLog(@"id: %i",flickrID);
    return flickrID;
}

控制台的输出为 :

2012-05-26 13:30:25.771 TestApp[1744:f803] flickr id string: 5629026092
2012-05-26 13:30:25.773 TestApp[1744:f803] id: 2147483647

为什么调用 intValue 来改变实际号码?

最佳回答

使用 long long 代替,您的人数大于您所能处理的 >int (最大值为 2147483647 ,您可以在第二个日志中看到)

问题回答

您的值太大, 无法以 32 位表示。 您可以存储的最大值是 2147483647。 对于未签名的整数, 您需要转换为长长的整数, 以代表5629026092。

您可能需要为此创建一个编号。 我不是数字格式师专家, 并且总是要挖掘文档才能找到如何使用它们 。

我刚刚试过了,这个代码起作用了:

  NSString *numberString = @"5629026092";

  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

  NSNumber *number = [formatter numberFromString: numberString];
  long long value = [number longLongValue];
  NSLog(@"%@ = %qi", numberString, value);
  [formatter release];

您也可以将字符串转换为 C 字符串, 并使用扫描仪, 想想看 。

易 : INT_MAX 型号为 2147483647 的变量的最大值

我发现这个方法很方便:

NSString *flickIDString = [dirtyFlickrIdArray objectAtIndex:0]; // read some huge number into a string

// read into a NSNumber object or a long long variable. you choose
NSNumber *flickIDNumber = flickIDString.longLongValue;
long long flickIDLong = flickIDString.longLongValue;




相关问题
How do you create UIBarButtonItems with a radio interface?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up ...

iPhone settings bundle

I want to allow the user to enter a valid date using the iPhone’s settings application. I have experimented with many of the PreferenceSpecifiers data node types including date. I have two issues: ...

Circular #import/@class problem in ObjectiveC

I m going to use an example to properly illustrate my confusion. I can t quite wrap my head around this. In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Cocoa-Touch: issue looping MPMoviePlayerController

I have an app which has to load some data at startup, so I want to display a splash-screen animation. I m using the MPMoviePlayerController to play a m4v file. The movie has it s background set to [...

Iphone sequential animation with setAnimationDelay

I m trying to chain animation events. The application I m coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "...

热门标签