How to Print Out(Nslog) The Properties of a Custom Object Added to a Nsmutablearray

How to Print out(NSLog) the properties of a custom object added to a NSMutableArray

You have to use the description method inside your Person class

-(NSString *)description{

return @"FirstName: %@, LastName: %@, E-mail: %@",
_firstName, _lastName, _email;
}

This way you can print always the object you have inside your NSArray but instead of the memory description you'll get returned the values you've defined before in your description method of the specific object.

If you just want to do this with the element from the NSArray use placeholders:

NSLog(@"FirstName: %@, LastName: %@, E-mail: %@", 
obj.firstname, obj.lastname, obj.email);

There is not much difference between, but its more useful because you don't have to rewrite it once you have created your description method, you just have to print the object.

Printing a string object from an NSMutableArray

I'm guessing the Fraction type you created has a NSString property or method named number (to match the -setNumber: method), in which case you would use the following code to print it:

NSLog("%@", [myArrayElement number]);

Or, for the second loop:

NSLog("%@", [myArray[i] number]);

How to print an object variables from a NSMutableArray?

The code you posted is fine as-is. There is no equivalent in Objective-C / Cocoa to Java's typed collections. You need to cast the results.

Actually, there is a little trick you can do:

NSLog(@"Value: %@", [myStudentRepo.studentRepository[0] valueForKey:@"name"]);

Not able to print a NSMutableArray in objective c ios

When you use NSLog to print an object the way you did, it automatically prints the details it thinks are interesting to you about that object (in your case, the class name and the address). You want the content of the array's elements, in which case you should loop through the array and print each of the elements. For example, if your array contains NSStrings, you should do something like this:

for (NSString *curElement in sortedWords)
{
NSLog(@"%@", curElement);
}

If your elements are custom objects or any other kind of object, you can, of course, format the NSLog accordingly.

Hope this helps...

Edit:

Following your comment, for your specific case, the loop would look something like this:

for (WCWord *curElement in sortedWords)
{
// Assuming you want to print the 'count' property and it's int
NSLog(@"%d", curElement.count);

// You can, of course, use any other field (or several of them)
}

How to access a property of an object from an NSMutableArray

The issue is you're not getting the correct object out of your array. If you break right after you get your CardView out of your array, and print the description in the console, it's a string and not actually a CardView. There are two things you need to do.

  1. Make sure you only add CardViews to your array. Double check this.
  2. In your for loop you can do this to make sure you are only setting frames on CardViews:

    if([card isMemberOfClass:[CardView class]]) {
    card.frame = CGRectMake(10+70*i, 340, 60, 85);
    }

You still want to make sure that you're actually only putting in CardViews though, but this is a way to debug.

Is my NSMutablearray adding the objects and my NSLog the correct number of objects in the array?

A prettier design for this would be to use tags and a dictionary instead of if-else, so

// assign tags:
checkButton1.tag = 1;
checkButton2.tag = 2;

// etc., then setup data beforehand
NSDictionary *mapTags = @{ @1:@"Freshman", @2:@"Sophomore" /* etc */ };
NSMutableArray *arrayOfselections = [NSMutableArray array];

// then look how small this gets:
-(IBAction)checkBoxButtonHandler:(id)sender {

NSNumber *tagNumber = [NSNumber numberWithInt:sender.tag];
[self.arrayOfselections addObject:self.mapTags[tagNumber]];
}

Put all object properties into an NSDictionary or NSArray

It's easy to get an array of declared properties using the class_copyPropertyList and property_getName functions in the Objective-C runtime. Here's one such implementation:

- (NSArray *)properties
{
NSMutableArray *propList = [NSMutableArray array];
unsigned int numProps = 0;
unsigned int i = 0;

objc_property_t *props = class_copyPropertyList([TestClass class], &numProps);
for (i = 0; i < numProps; i++) {
NSString *prop = [NSString stringWithUTF8String:property_getName(props[i])];
[propList addObject:prop];
}

return [[propList copy] autorelease];
}

You could add this as a method in a category on NSObject. Here's a full code listing that can be compiled that demonstrates this.

I'm not sure how'd you do it with an NSDictionary, only because I'm not sure what you expect the key-value pairs to be.

get value of class property (NSMutableArray)

NSString is an object and can be print with %@ and not %lu. Update your code and you're with your name.

for (int i = 0; i < 2; ++i) {
Zoep *player = (Zoep *)[self.zoepers objectAtIndex:i];
NSLog(@"Naam: %@", player. playername);
}

Conclusion: You're storing Zoep class object into your zoepers array, so at the time of accessing zoepers you would first make reference of Zoep class object and then can access properties of that class.

BTW, indexOfObject: takes an object and will return index (position in array).



Related Topics



Leave a reply



Submit