开发者

How can I see values of Object in NSLog?

Suppose I have an object containing some data.

How can I see that data using NSLog?

If anyone is not clear about my开发者_StackOverflow中文版 question, then can ask me again.


If you want to see an NSArray and NSDictionary and etc objects then you can directly print like NSLog(@"%@",object);

If it is an user defined object then you need to display by calling with property (attribute).

User defined object with name object and properties like

NSString *property1;
int property2;
NSMutableArray *property3;

Print them in the console as follows:

NSLog(@"%@, %d, %@" object.property1,object.property2,object.property3);


If you implement the -(NSString*)description method in your class then you can use NSLog to output a summary of the data. Of course, you can also directly output any property.

For example:

NSLog (@"%@ %d", object, object.integer);

The first part calls the description method and outputs that; the second part gets the value of the integer property of object and outputs that.


Every Objective-c Object (this comes from NSObject) has a property called description. So if you want to print information about your class this is the way to go.

@implementation MyClass

- (NSString*)description
{
   return [NSString stringWithFormat:@"MyClass:%@", @"This is my class"];
}

so if you do a call like this.

MyClass *myClass = [[MyClass alloc] init];
NSLog(@"%@", myClass);
NSLog(@"%@", [myClass description]); //Same as the line above

Then it will write "MyClass:This is my class" to the console (in this case it will print it twice).


Implement description of the given class.

-(NSString*)description {

    return [NSString
            stringWithFormat:@"<%@> name: `%@` size: `%@`",
            NSStringFromClass(self), self.name,
            NSStringFromCGSize(self.size)];
}

NSLog(@"%@", object); // <Object> name: `Harry` size: `{2, 2}`

extension Object: CustomStringConvertible {
    
    var description: String {
        "<\(Self.self)> name: `\(name)` size: `\(size)`"
    }
}

print(object) // <Object> name: `Harry` size: `(2.0, 2.0)`


I would suggest these:

Objects:

For objects like Dictionary, Array, Strings do it like:

NSLog(@"%@", object);

For basic data-types like integers

NSLog(@"%i",intVal);

For type encoding you should see http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html


Use this class https://github.com/arundevma/ICHObjectPrinter

NSLog(@"Object description is %@",[ICHObjectPrinter descriptionForObject:person]);


NSLog(@"My object data:%@",[myObj someData]);
NSLog(@"My object Other data:%@",[myObj someOtherData]);

Or directly:

NSLog(@"%@",myObj);
NSLog(@"Description:%@",[myObj description]);


Additionally to Satya's answer, if you want to see basic c data types, use the format specifiers. Such as %d for an integer:

NSLog (@"My integer:%d", myObject.myInteger);

The complete list is here:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜