Reputation: 2298
What I am trying to do is have Schedule class containing multiple Class classes. just like in real life, a schedule contains several classes(lectures) in it.
I created this Schedule.h and .m :
in Schedule.h :
#import <Foundation/Foundation.h>
#import "myClass.h"
@interface Schedule : NSObject
@property (nonatomic, strong) NSMutableArray *containsClassArr;
@end
and containsClassArr contains myClass.h objects.
So when I enumerate and NSLog each item in containsClassArr:
for(myClass *myclass in containsClassArr){
NSLog("This Schedule contains class: %@", myclass);
}
the NSLog I get is:
This Schedule contains class: <myClass: 0x7fb78b4ab660>
This Schedule contains class: <myClass: 0x7fb78b493370>
This Schedule contains class: <myClass: 0x7fb78b4a7350>
This Schedule contains class: <myClass: 0x7fb78b4ab4c0>
Now, inside myClass.h, I have a variable like this:
#import <Foundation/Foundation.h>
@interface myClass : NSObject
@property (nonatomic, strong) NSString *classTime;
@end
How can I access NSString *classTime?
I know this is wrong, but in my head I am trying to do something like this:
Schedule *newSchedule = [[Schedule alloc]init];
//assuming containsClassArr in newSchedule contains myClass objects already.
NSLog(@"Classes for Monday are:");
NSLog(@"class time -> %@", newSchedule.containsClassArr.myClass.classTime);
get:
Classes for Monday are:
class time -> 9am
class time -> 10am
class time -> 3pm
class time -> 5pm
1) What would be the correct access method for this type of .. access?
2) Is this kind of structure okay? or is there other recommended way?
Upvotes: 0
Views: 389
Reputation: 57114
You would need to specify on what member of the array you want to access the class property
int index = 0;
NSString* classTime = [newSchedule.containsClassArr[index] classTime];
You can either use the []
syntax which would not require a cast or you cast the returned value from the array and use the .
syntax.
NSString* classTime = ((myClass*)newSchedule.containsClassArr[index]).classTime;
In both cases you have to assure that your index is within the array bounds.
If you want to iterate over the array you can access the property just as easy
for (myClass* myclass in containsClassArr) {
NSLog("This Schedule contains class: %@ withValue: %@", myclass, myclass.classTime);
}
Upvotes: 4
Reputation: 241
I would recommend using the following as it will be safe for nullable values and contains the index to for debugging purposes
[self.containsClassArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSString class]])
{
if (myclass.classTime) {
NSLog("This Schedule contains class: %@ withValue: %@ at Position %i", myclass, myclass.classTime , idx);
}
else {
NSLog("This Schedule contains error at index: %i" , idx);
}
}
}];
Upvotes: 0
Reputation: 122391
Exposing a mutable array is very poor:
@property (nonatomic, strong) NSMutableArray *containsClassArr;
As the containing (Schedule
) instance probably wants to know when classes are added/removed and so you should only allow the Schedule
class to change the class schedule (the word "class" is confusing to talk about in OO programming).
Next you need to use the correct data type and using a string to hold a time is not using the correct data type:
NSString *classTime;
However WRT to accessing this property, what you have is OK, except that you probably just want to expose an immutable array of the classes...
Upvotes: 1
Reputation: 2044
You'd use KVC see here and there:
Try this:
NSArray *classTimeArray = [containsClassArr valueForKeyPath:@"classTime"];
Upvotes: 0