2012-01-26 30 views
24

Làm thế nào để viết phương pháp mô tả thích hợp cho một lớp học?Làm thế nào để viết phương pháp mô tả thích hợp cho một lớp học?

tôi đã thực hiện

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

} 

Evrey điều là tốt nếu tôi gọi mô tả về đối tượng của tôi. Nhưng nếu tôi có một mảng của các đối tượng và tôi gọi mô tả về nó tôi nhận được:

"Name: Alex \ n Địa chỉ: số địa chỉ \ n",

Những gì tôi muốn nhận được là

"Name: Alex

địa chỉ: số địa chỉ"

+1

bạn sẽ phải lặp trên các yếu tố và gọi 'description' trên mỗi. Mô tả mảng được định dạng đầu ra, có thể ngắn mạch một số thứ như "\ n", để giữ định dạng phù hợp –

+1

xem [http://stackoverflow.com/a/1828689/971401](http://stackoverflow.com/a/1828689/971401). Câu trả lời có thể có thể giúp bạn. –

Trả lời

12

Tôi đào sâu hơn một chút trong các khung công tác iOS và tôi đã quan sát thấy hành vi mặc định của mô tả sdk iOS không phải là để đặt "\ n" nhưng ";".

Ví dụ:

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]); 

thỏa ra là:

FontDescription: font-family: "Helvetica"; font-weight: bình thường; font-style: bình thường; 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" 

)

Vì vậy, tôi đã quyết định sử dụng phương pháp tương tự với lớp học của tôi.

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

} 

Và đặt ra sẽ là:

"Name: Alex; Địa chỉ: số địa chỉ;"

Đối với đối tượng, bản thân nó.

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;" 

)

Đối với một mảng của các đối tượng.

18

Ngoài ra bạn có thể sử dụng một trở về vận chuyển \r mà sẽ kết thúc trong một dòng mới quá (ngay cả trong một mô tả NSArray)

+0

nó hoạt động, thx rất nhiều –

+1

Câu trả lời tuyệt vời! Mặc dù tôi vẫn không thể làm việc, vì vậy tôi đã kết thúc bằng "" (4 dấu cách). –

+0

Đã làm việc cho tôi, Cảm ơn –

Các vấn đề liên quan