0

From what I've read NSMutableArray adds objects.

How can I print the Student object variables from a given position without casting the object as a Student.

I'm looking for something like ArrayList<Student> in Java so i can easily print ArrayList.get(i).getName, ArrayList.get(i).getPrice .

    StudentRepository* myStudentRepo = [[StudentRepository alloc]init];

    Student* myStudent = [[Student alloc]init];

    myStudent.name = @"John";

    // add a Student to the NSMutableArray
    [myStudentRepo.studentRepository addObject:myStudent];

    NSLog(@"Value: %@", myStudentRepo.studentRepository);

    for(Student* myStudentItem in myStudentRepo.studentRepository)
    {
        NSLog(@"Value: %@", myStudentItem.name);
    }

    // print the Student from a given position
    NSLog(@"Value: %@", [(Student*)[myStudentRepo.studentRepository objectAtIndex:0] name]);
1
  • Thanks all for the help ! Commented Feb 21, 2013 at 21:40

5 Answers 5

2

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"]);
Sign up to request clarification or add additional context in comments.

2 Comments

+1, Or it can be used as [myStudentRepo.studentRepository[0] name]. It shouldn't show any warning.
@ACB True since you end up calling name on an id object. As long as the compiler has seen a name method from somewhere, it will be happy.
1

You can use KVC (Key Value Coding) to access the properties of an object without casting it:

[[myStudentRepo.studentRepository objectAtIndex:0] valueForKey:@"name"];

See: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html#//apple_ref/doc/uid/20002170-BAJEAIEE

Comments

1

You could override description or debugDescription in you Student class:

Since I don't the make up of your student, please allow the following example of a straight forward way:

// could also be -(NSString*)debugDescription    
- (NSString *)description {
      return [NSString stringWithFormat:@"Prop1: %@ \nIntVal1: %d\nfloatVal1 = %3.2f", self.prop1, self.intVal1, self.floatval1];
}

This gets tedious with large and complex objects, though.

Comments

1

You Can use something like this

[(Student*)myStudentRepo.studentRepository[0] name];

Or you can override the description of the Student like this: in Student.m add this :

-(NSString *)description{
        return [NSString stringWithFormat:@"Student Name:%@", self.name];
     }

Whenever you need to print the Student just type:

NSLog(%@, student);

Comments

1

If you wish to ensure your collection actually only contains Student objects, the equivalent of Java's parametric collection you can do this. See this question for a solution for dictionaries, an array solution would be similar. You can combine the accepted solution to that question with typed getters & setters to avoid any casts.

Alternatively if you are not actually concerned over ensuring only Student objects can be added you can write an extension or category which adds a typed getter or setter - this just calls the standard setter or getter adding an casts as needed. You'll see this approach in the answers to the above question as well.

(No code here as you'll find all you need in the other question.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.