從我讀的NSMutableArray
添加對象。如何從NSMutableArray打印對象變量?
如何從給定位置打印Student
對象變量而不將對象作爲Student
投射。
我在找Java like ArrayList<Student>
這樣的東西,所以我可以輕鬆打印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]);
謝謝大家的幫助! – Mythul 2013-02-21 21:40:31