技术CTO-关注编程入门知识,提供编程入门教程

您的位置: 首页 > 移动开发 > ios > 正文

Object-C字符串拼接问题

来源: 技术CTO 阅读:

//这一段是ViewController.m中的一段,是计算器小程序中,按下数字键在show的label中显示的
//想把按下的数字做成字符串并转为double,用于计算
- (IBAction)digPress:(id)sender {
    //UIButton *btm=(UIButton*)sender;
    
    if (_show.text==nil) {
        _show.text=[sender currentTitle];
    }
    [Mystring appendString:[sender currentTitle]];//这段追加代码会崩溃
    Mystring=[sender currentTitle];
   
    _show.text=Mystring;
    
}

以下是log:

2016-03-25 18:22:57.882 Mycalculator[785:55329] -[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000000000381
2016-03-25 18:22:57.906 Mycalculator[785:55329] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000000000381'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010f28fd85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010ed03deb objc_exception_throw + 48
2   CoreFoundation                      0x000000010f298d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010f1decfa ___forwarding___ + 970
4   CoreFoundation                      0x000000010f1de8a8 _CF_forwarding_prep_0 + 120
5   Mycalculator                        0x000000010e801350 -[ViewController digPress:] + 256
6   UIKit                               0x000000010f647a8d -[UIApplication sendAction:to:from:forEvent:] + 92
7   UIKit                               0x000000010f7bae67 -[UIControl sendAction:to:forEvent:] + 67
8   UIKit                               0x000000010f7bb143 -[UIControl _sendActionsForEvents:withEvent:] + 327
9   UIKit                               0x000000010f7ba263 -[UIControl touchesEnded:withEvent:] + 601
10  UIKit                               0x000000010f6ba99f -[UIWindow _sendTouchesForEvent:] + 835
11  UIKit                               0x000000010f6bb6d4 -[UIWindow sendEvent:] + 865
12  UIKit                               0x000000010f666dc6 -[UIApplication sendEvent:] + 263
13  UIKit                               0x000000010f640553 _UIApplicationHandleEventQueue + 6660
14  CoreFoundation                      0x000000010f1b5301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15  CoreFoundation                      0x000000010f1ab22c __CFRunLoopDoSources0 + 556
16  CoreFoundation                      0x000000010f1aa6e3 __CFRunLoopRun + 867
17  CoreFoundation                      0x000000010f1aa0f8 CFRunLoopRunSpecific + 488
18  GraphicsServices                    0x0000000112b0fad2 GSEventRunModal + 161
19  UIKit                               0x000000010f645f09 UIApplicationMain + 171
20  Mycalculator                        0x000000010e801aef main + 111
21  libdyld.dylib                       0x0000000111a6992d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

NSMutableString 才有 appendString 方法吧,类型改一下就行不影响使用
楼上说的没错,可以试试string = [string appendString:.....]
// 将字符串内容转换成 double 类型
[@"123.123" doubleValue];
NSLog(@"%f",[@"123.123" doubleValue]);
1楼正解。楼主可试试

^_^ 如果您热爱技术、热爱编程,想与更多的朋友一起交流学习,欢迎加入本站官方QQ群:345733473 ^_^