How can I see values of Object in NSLog?
Asked Answered
H

8

29

Suppose I have an object containing some data.

How can I see that data using NSLog?

If anyone is not clear about my question, then can ask me again.

Hypochondrium answered 28/2, 2011 at 10:40 Comment(0)
G
37

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

Greaten answered 28/2, 2011 at 10:48 Comment(0)
A
20

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.

Actinometer answered 28/2, 2011 at 10:51 Comment(1)
I knew I have to implement a function to get it printed. This is exactly what I'm looking for. Upvoted. Thanks!Intervocalic
D
11

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).

Device answered 28/2, 2011 at 10:53 Comment(0)
S
10

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)`
Syncretism answered 7/5, 2014 at 23:24 Comment(0)
T
3

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

Tourism answered 28/2, 2011 at 10:51 Comment(0)
G
2

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

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

Grime answered 7/7, 2014 at 17:47 Comment(0)
U
1
NSLog(@"My object data:%@",[myObj someData]);
NSLog(@"My object Other data:%@",[myObj someOtherData]);

Or directly:

NSLog(@"%@",myObj);
NSLog(@"Description:%@",[myObj description]);
Unhandy answered 28/2, 2011 at 10:50 Comment(0)
H
0

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/

Hauteloire answered 28/2, 2011 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.