Re: [問題] - (NSString *)description 中文印不出來
※ 引述《ratbert (鼠伯特)》之銘言:
: obj-c 新手請問
: 我有一個 instance variable type 是 id:
: @interface A : NSObject
: @property (retain) id item;
: @end
: 然後我幫這個 class implement 一個簡單的 description:
: @implementation A
: @synthesize item;
: - (NSString *)description
: {
: return [NSString stringWithFormat:@"(%@)", self.item];
: }
: @end
: 我把一個 NSString assign 給 id 這個 type 的 instance variable
: 然後我想印 A 這種 object:
: A *a = [[A alloc] init];
: a.item = @"8月";
: NSLog(@"%@", a);
: 印出來的東西變成類似這樣:
: "(8\U6708)"
: 請問這是為什麼呢?
發現跟 NSArray 有關
修改一下 uranusjr 網友的 source code
----------->8----------->8-----------
#import <Foundation/Foundation.h>
@interface ObjectA : NSObject
@property (retain) id item;
@end
@implementation ObjectA
@synthesize item;
- (NSString *)description
{
return [NSString stringWithFormat:@"(%@)", [self item]];
}
@end
NSArray *test(void)
{
ObjectA *obj = [[[ObjectA alloc] init] autorelease];
obj.item = @"8月";
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
[array addObject:obj];
return array;
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = test();
NSLog(@"%@", array);
[pool drain];
return 0;
}
-----------8<-----------8<-----------
這樣的話印出來的結果是
2011-11-20 17:27:45.615 TestUTF8[2154:207] (
"(8\U6708)"
)
是不是問題出在 NSArray 本身的 description method 就是會印成這樣咧?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.37.145.173
→
11/20 18:25, , 1F
11/20 18:25, 1F
→
11/20 19:14, , 2F
11/20 19:14, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章