Jaki jest odpowiednik Objective-C dla "toString ()", do użycia z NSLog?
Czy istnieje metoda, którą mogę nadpisać w moich niestandardowych klasach tak, że gdy
NSLog(@"%@", myObject)
Jest wywołany, wyświetli pola (lub cokolwiek uznam za ważne) mojego obiektu? Chyba Szukam ekwiwalentu Objective-C toString()
Javy.
5 answers
Jest to metoda instancji description
, zadeklarowana jako:
- (NSString *)description
Oto przykładowa implementacja (dzięki grahamparks):
- (NSString *)description {
return [NSString stringWithFormat: @"Photo: Name=%@ Author=%@", name, author];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-27 19:01:17
Dodaj to do @implementation
swojej klasy Zdjęć:
- (NSString *)description {
return [NSString stringWithFormat:@"Photo: Name=%@ Author=%@",name,author];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-09 15:57:15
Możesz nadpisać metodę opisu NSObject:
- (NSString *)description
W temacie logowania polecam ten wpis na blogu dla lepszego logowania Objective-C.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-09 15:51:43
Istnieją dwie funkcje, których możesz użyć.
- (NSString*)description
Zostanie to wyświetlone, gdy umieścisz swój obiekt jako, tj. parametr dla NSLog
. Inna funkcja opisu to:
- (NSString*)debugDescription
Zostanie wywołana, gdy wykonasz po anInstanceOfYourClass
w oknie Polecenia debugowania. Jeśli twoja klasa nie ma funkcji debugDescription
, zostanie wywołana tylko description
.
Zauważ, że klasa bazowa NSObject
ma zaimplementowaną description
, ale jest dość prosta: wyświetla tylko adres obiektu. Dlatego zalecam zaimplementowanie description
W dowolnej klasie, z której chcesz uzyskać informacje, szczególnie jeśli używasz metody description
w swoim kodzie. Jeśli używasz description
w swoim kodzie, sugeruję zaimplementowanie debugDescription
, a także uczynienie debugDescription
bardziej wyrazistym.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-23 01:26:57
Wyświetli dostępne Głosy:
NSLog((@"speechVoices:%", [[AVSpeechSynthesisVoice speechVoices] description] ));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-12 07:04:51