如何为类编写正确的描述方法?

Ale*_*nte 24 methods objective-c ios4 ios

如何为类编写正确的描述方法?

我已经实施了

- (NSString *)description {
     NSString *descriptionString = [NSString stringWithFormat:@"Name: %@ \n Address: %@ \n", self.name, self.address];
     return descriptionString;

}
Run Code Online (Sandbox Code Playgroud)

如果我在我的对象上调用描述,Evrey的事情就好了.但是如果我有一个对象数组并且我在其上调用描述,我会得到:

"姓名:Alex \n地址:某地址\n",

我想得到的是

"姓名:亚历克斯

地址:一些地址"

小智 18

此外,您还可以使用回车符\r,该回车符也将以新行结束(即使在NSArray描述中)


Ale*_*nte 12

我在iOS框架中挖掘了一些,我发现iOS sdk描述的默认行为不是放置"\n"而是";".

例:

    UIFont *font = [UIFont systemFontOfSize:18];
    NSLog(@"FontDescription:%@",[font description]);

    NSMutableArray *fontsArray = [NSMutableArray arrayWithCapacity:0];
    for(int index = 0; index < 10; index++) {
        [fontsArray addObject:font];
    }
    NSLog(@"FontsArrayDescription:%@",[fontsArray description]);
Run Code Online (Sandbox Code Playgroud)

出局是:

FontDescription:font-family:"Helvetica"; font-weight:normal; font-style:normal; font-size:18px

FontsArrayDescription :(

 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",    
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px"  
Run Code Online (Sandbox Code Playgroud)

)

所以我决定在课堂上使用相同的方法.

- (NSString *)description {
     NSString *descriptionString = [NSString stringWithFormat:@"Name: %@; Address: %@;", self.name, self.address];
     return descriptionString;

}
Run Code Online (Sandbox Code Playgroud)

输出将是:

"姓名:亚历克斯;地址:一些地址;"

对象是自我.

objecsArrayDescription :(

 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;" 
Run Code Online (Sandbox Code Playgroud)

)

对于一个对象数组.