【发布时间】:2011-06-19 03:53:35
【问题描述】:
这很难描述,但我目前正在从数据库中捕获一个字符串,这个字符串可以是 1-4 个字符长,但是我希望始终显示 4 个字符,所以如果我得到一个返回的字符串是 34 ,我希望它是 0034。
我已经设置了一个捕获字符串的方法,所以现在我只需要弄清楚如何做到这一点。然后我打算做的是将该字符串输入到 NSArray 中,这样我就可以将数组的每个 [i'th] 发送到 4 个不同的方法来控制我的应用程序中的动画。
它采用字符串格式的原因是因为应用程序中的各种格式化原因,我不得不将它从十六进制反弹到整数到字符串。
这是我到目前为止的代码。建议/解决方案将非常感谢,我太新了,很难找到字符串操作等问题的解决方案。
//... other method I am getting the string from/.
[self formatMyNumber:dataString];
///..
-(void)formatMyNumber:(NSString *)numberString{
//resultLabel.text = numberString; //check to make sure string makes it to here.
//NSLog(@"hello From formatMyNumber method"); //check
}
//..
//the with send off each character to 4 animation methods that accept integers.
- (void)playAnimationToNumber:(int)number{
//...
//更新...奇怪的事情发生了。 到目前为止,这是我的方法。
//Number Formatter
-(void)formatMyNumber:(NSString *)numberString{
NSLog(@"This is what is passed into the method%@",numberString);
int tempInt = (int)numberString;
NSLog(@"This is after I cast the string to an int %i",tempInt);
//[NSString alloc] stringWithFormat:@"%04d", numberString];
NSString *tempString = [[NSString alloc] initWithFormat:@"%04d", tempInt];
NSLog(@"This is after I try to put zeros infront %@",tempString);
//resultLabel.text = tempString;
//NSLog(@"hello From formatMyNumber method");
}
这是输出。
[会议于 2011-06-19 开始 16:18:45 +1200。] 2011-06-19 16:18:54.615 日产代码0.1[4298:207] 731 2011-06-19 16:18:54.616 日产代码0.1[4298:207] 79043536 2011-06-19 16:18:54.617 日产代码0.1[4298:207] 79043536 2011-06-19 16:18:54.617 nissanCode0.1[4298:207] 你好来自 formatMyNumber 方法
【问题讨论】:
标签: iphone ios nsstring nsarray int