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.

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

8 Answers8

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Satya
  • 3,320
  • 1
  • 20
  • 19
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.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • I knew I have to implement a function to get it printed. This is exactly what I'm looking for. Upvoted. Thanks! – Matt Chuang Dec 01 '11 at 00:01
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).

Konrad77
  • 2,515
  • 1
  • 19
  • 36
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)`
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
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

Nandita Saini
  • 47
  • 1
  • 6
2

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

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

arundevma
  • 933
  • 7
  • 24
1
NSLog(@"My object data:%@",[myObj someData]);
NSLog(@"My object Other data:%@",[myObj someOtherData]);

Or directly:

NSLog(@"%@",myObj);
NSLog(@"Description:%@",[myObj description]);
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
SNR
  • 1,249
  • 2
  • 20
  • 38
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/

Dave
  • 3,438
  • 20
  • 13