Mục tiêu-C, 346 byte
-(void)printTernaryOfInt:(int)ternary{NSMutableString *outString=@"".mutableCopy; for (int i=1;i<=ternary;i++) {[outString appendString:[NSString stringWithFormat:@" ? %i",i]];}[outString deleteCharactersInRange:NSMakeRange(0, 2)];for (int i=1;i<ternary;i++) {[outString appendString:[NSString stringWithFormat:@" : 0"]];}NSLog(@"%@",outString);}
Đặt vào trong 0 cho inthoặc bất cứ điều gì tiêu cực tăng lên NSRangeExceptiondo outStringcó chứa nil. Điều này sẽ chạy trên iOS 2.0 trở lên và nhiều phiên bản mới nhất của Mac OS X.
Phân tích mã:
-(void)printTernaryOfInt:(int)ternary{ ... }
Khai báo hàm tiêu chuẩn trong Objective-C.
NSMutableString *outString=@"".mutableCopy;
Tạo một chuỗi cho đầu ra để đi đến, outString , và làm cho nó có thể thay đổi. (Nói cách khác, nó có thể được đọc và viết.
for (int i=1;i<=ternary;i++) {[outString appendString:[NSString stringWithFormat:@" ? %i",i]];}
Thêm phần đầu tiên của chuỗi vào đầu ra.
[outString deleteCharactersInRange:NSMakeRange(0, 2)];
Dọn dẹp phần đầu của chuỗi để đảm bảo ? 1được thay thế bằng 1. Lưu ý: nếu 0được đưa ra, đây là nơi NSRangeExceptionsẽ xảy ra, do không có chỉ mục1 .
for (int i=1;i<ternary;i++) {[outString appendString:[NSString stringWithFormat:@" : 0"]];}
Thêm phần thứ hai của chuỗi vào chuỗi.
NSLog(@"%@",outString);}
Nhổ chuỗi ra ngoài bằng cách sử dụng NSLogvà đóng chức năng.
Đầu ra:
Nhập vào 0cung cấp nhật ký sự cố này:
2015-07-11 05:15:28.036 Example App[41665:2134488] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString deleteCharactersInRange:]: Range or index out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x009b5746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0063ea97 objc_exception_throw + 44
2 CoreFoundation 0x009b566d +[NSException raise:format:] + 141
3 CoreFoundation 0x00981813 mutateError + 259
4 CoreFoundation 0x009818c1 -[__NSCFString deleteCharactersInRange:] + 65
5 Example App 0x000e3785 -[ViewController printTernaryOfInt:] + 277
6 Example App 0x000e3645 -[ViewController placeOrder:] + 133
7 libobjc.A.dylib 0x006547cd -[NSObject performSelector:withObject:withObject:] + 84
8 UIKit 0x00d75a40 -[UIApplication sendAction:to:from:forEvent:] + 99
9 UIKit 0x00d759d2 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
10 UIKit 0x00eb613a -[UIControl sendAction:to:forEvent:] + 69
11 UIKit 0x00eb6557 -[UIControl _sendActionsForEvents:withEvent:] + 598
12 UIKit 0x00eb57c1 -[UIControl touchesEnded:withEvent:] + 660
13 UIKit 0x00dcdcaa -[UIWindow _sendTouchesForEvent:] + 874
14 UIKit 0x00dce786 -[UIWindow sendEvent:] + 792
15 UIKit 0x00d8c681 -[UIApplication sendEvent:] + 242
16 UIKit 0x00d9cab8 _UIApplicationHandleEventFromQueueEvent + 21484
17 UIKit 0x00d702e7 _UIApplicationHandleEventQueue + 2300
18 CoreFoundation 0x008d706f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
19 CoreFoundation 0x008ccb7d __CFRunLoopDoSources0 + 253
20 CoreFoundation 0x008cc0d8 __CFRunLoopRun + 952
21 CoreFoundation 0x008cba5b CFRunLoopRunSpecific + 443
22 CoreFoundation 0x008cb88b CFRunLoopRunInMode + 123
23 GraphicsServices 0x029e42c9 GSEventRunModal + 192
24 GraphicsServices 0x029e4106 GSEventRun + 104
25 UIKit 0x00d740b6 UIApplicationMain + 1526
26 Example App 0x000e3cfa main + 138
27 libdyld.dylib 0x02d76ac9 start + 1
28 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
1 đưa ra điều này:
2015-07-11 05:06:02.360 Example App[41665:2134488] 1
2 đưa ra điều này:
2015-07-11 05:06:07.613 Example App[41665:2134488] 1 ? 2 : 0
7 đưa ra điều này:
2015-07-11 05:06:12.147 Example App[41665:2134488] 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 : 0 : 0 : 0 : 0 : 0 : 0
200 đưa ra điều này:
2015-07-11 05:06:35.552 Example App[41665:2134488] 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 ? 10 ? 11 ? 12 ? 13 ? 14 ? 15 ? 16 ? 17 ? 18 ? 19 ? 20 ? 21 ? 22 ? 23 ? 24 ? 25 ? 26 ? 27 ? 28 ? 29 ? 30 ? 31 ? 32 ? 33 ? 34 ? 35 ? 36 ? 37 ? 38 ? 39 ? 40 ? 41 ? 42 ? 43 ? 44 ? 45 ? 46 ? 47 ? 48 ? 49 ? 50 ? 51 ? 52 ? 53 ? 54 ? 55 ? 56 ? 57 ? 58 ? 59 ? 60 ? 61 ? 62 ? 63 ? 64 ? 65 ? 66 ? 67 ? 68 ? 69 ? 70 ? 71 ? 72 ? 73 ? 74 ? 75 ? 76 ? 77 ? 78 ? 79 ? 80 ? 81 ? 82 ? 83 ? 84 ? 85 ? 86 ? 87 ? 88 ? 89 ? 90 ? 91 ? 92 ? 93 ? 94 ? 95 ? 96 ? 97 ? 98 ? 99 ? 100 ? 101 ? 102 ? 103 ? 104 ? 105 ? 106 ? 107 ? 108 ? 109 ? 110 ? 111 ? 112 ? 113 ? 114 ? 115 ? 116 ? 117 ? 118 ? 119 ? 120 ? 121 ? 122 ? 123 ? 124 ? 125 ? 126 ? 127 ? 128 ? 129 ? 130 ? 131 ? 132 ? 133 ? 134 ? 135 ? 136 ? 137 ? 138 ? 139 ? 140 ? 141 ? 142 ? 143 ? 144 ? 145 ? 146 ? 147 ? 148 ? 149 ? 150 ? 151 ? 152 ? 153 ? 154 ? 155 ? 156 ? 157 ? 158 ? 159 ? 160 ? 161 ? 162 ? 163 ? 164 ? 165 ? 166 ? 167 ? 168 ? 169 ? 170 ? 171 ? 172 ? 173 ? 174 ? 175 ? 176 ? 177 ? 178 ? 179 ? 180 ? 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