2012-01-26 29 views
24

クラスに適切な説明メソッドを書き込む方法は?私は私のオブジェクトの記述を呼び出す場合クラスに適切な説明メソッドを書き込む方法は?

私が実装した

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

} 

Evreyの事は結構です。私は、オブジェクトの配列を持っていると私はそれに説明を呼び出​​す場合しかし、私は得る:

を「名前:アレックス\ nは住所:一部のアドレスの\ n」は、

私が取得したいと思い何があります

"名:アレックス

住所:いくつかのアドレス"

+1

を使用することができます。配列の記述はフォーマットされた出力であり、書式の整合性を維持するために、「\ n」のようなものを短絡させる可能性があります。 –

+1

参照[http://stackoverflow.com/a/1828689/971401](http://stackoverflow.com/a/1828689/971401)。おそらくあなたを助けることができる答え。 –

答えて

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

アウトプットは次のとおりです。

たFontDescription:フォントファミリ: "ヘルベチカ"。 font-weight:normal;フォントスタイル:通常;フォントサイズ: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" 

だから私は私のクラスと同じアプローチを使用することを決定しました。

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

} 

そしてアウトプットは次のようになります。

"名:アレックス;住所:いくつかのアドレス。"

オブジェクト自体は自己です。

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

)オブジェクトの配列について

18

また、あなたは要素をループにあり、それぞれの `description`を呼び出します(でも、NSArrayの記述で)あまりにも新しい行になってしまいますキャリッジリターン\r

+0

それは、多くのthxが動作します –

+1

優れた答え!私はまだ仕事をすることができませんが、私は ""(4スペース)で終わりました。 –

+0

私のために働いた、ありがとう –

関連する問題