Reputation: 21
I have an array(taskDays) which I have stored in to class
which contains an array(datedTask) and a
NSdate(deadline), in the class array I store another class(datedTask obj) which contains a
NSString(title) and a NSDate(duedate).
When I try to NSLog deadline from main it works but when I try to NSLog
title or duedate xcode crashes.
I have included a picture so you guys can check it out.
If you want I can upload my code somewhere because I really don't know
which code to include here.
Thanks in advance!
Included the NSLog code since I think it might be the problem there:
NSLog(@"%@", [taskDay[0] title); //Makes stuff crash
NSLog(@"%@", [taskDay[0] datedTask); //How would I code to access the
//title in dated task?
LIUTaskDay.h
#import <Foundation/Foundation.h>
@class LIUDatedTask;
@interface LIUTaskDay : NSObject
@property (nonatomic, copy) NSArray *datedTask;
@property (nonatomic) NSDate *deadline;
- (void)addDatedTask:(LIUDatedTask *)d;
@end
LIUTaskDay.m
#import "LIUTaskDay.h"
#import "LIUDatedTask.h"
@interface LIUTaskDay ()
{
NSMutableArray *_datedTask;
}
@end
@implementation LIUTaskDay
- (void)setDatedTask:(NSArray *)d {
_datedTask = [d mutableCopy];
}
- (NSArray *)datedTask {
return [_datedTask copy];
}
- (void)addDatedTask:(LIUDatedTask *)d {
// Is datedTask nil?
if (!_datedTask) {
//Create the array
_datedTask = [[NSMutableArray alloc]init];
}
[_datedTask addObject:d];
d.taskDay = self;
}
@end
LIUDatedTask.h
#import <Foundation/Foundation.h>
#import "LIUSimpleTask.h"
@class LIUTaskDay;
@interface LIUDatedTask : LIUSimpleTask
@property (nonatomic) NSDate *dueDate;
@property (nonatomic) NSDate *completedDate;
@property (nonatomic, weak) LIUTaskDay *taskDay;
- (instancetype) initWithTitle:(NSString *)t
initWithDueDate:(NSDate *)dd;
@end
LIUDatedTask.m
#import "LIUDatedTask.h"
#import "LIUTaskDay.h"
@implementation LIUDatedTask
- (instancetype) initWithTitle:(NSString *)t
initWithDueDate:(NSDate *)dd;
{
if (self = [super initWithTitle:t]) {
_dueDate = [dd copy];
}
return self;
}
- (void)taskCompleted
{
[super taskCompleted];
_completedDate = [NSDate date];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "LIUDatedTask.h"
#import "LIUTaskDay.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDateComponents *components;
NSDate *date;
NSCalendar *gregorian;
NSMutableArray *taskDay = [NSMutableArray array];
NSString *string = [NSString stringWithFormat:@"Bok1"];
components = [NSDateComponents new];
[components setYear:2016];
[components setDay:21];
[components setMonth:1];
gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
date = [gregorian dateFromComponents:components];
LIUDatedTask *tmpLIUDatedTask = [[LIUDatedTask alloc]initWithTitle:string initWithDueDate:date];
LIUTaskDay *tmpLIUTaskDay = [[LIUTaskDay alloc]init];
[tmpLIUTaskDay addDatedTask:tmpLIUDatedTask];
components = [NSDateComponents new];
[components setYear:2017];
[components setDay:2];
[components setMonth:2];
gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
date = [gregorian dateFromComponents:components];
tmpLIUTaskDay.deadline = date;
[taskDay addObject:tmpLIUTaskDay];
//NSLog(@"%@, %@, %@", [day1[0] title],[day1[0] dueDate],[day1[0] deadline]);
NSLog(@"%@", [taskDay[0] title]);
}
return 0;
}
Here is the crash log:
2016-01-10 13:26:41.749 chrjo564_Ny[1072:41244] -[LIUTaskDay title]: unrecognized selector sent to instance 0x100603220
2016-01-10 13:26:41.751 chrjo564_Ny[1072:41244] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [LIUTaskDay title]: unrecognized selector sent to instance 0x100603220'
* First throw call stack:
(
0 CoreFoundation 0x00007fff8bd2003c exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8a2fe76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8bd230ad - [NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff8bc68e24 ___forwarding_ + 1028
4 CoreFoundation 0x00007fff8bc68998 _CF_forwarding_prep_0 + 120
5 objclabb4-chrjo564_Ny 0x0000000100002090 main + 816
6 libdyld.dylib 0x00007fff8a4ee5c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Upvotes: 0
Views: 208
Reputation: 3414
You taskDay
is an array for LIUTaskDay
class.
Your LIUDatedTask
is stored in the _datedTask
array of LIUTaskDay
.
To access LIUDatedTask
object, you need to call datedTask
method of LIUTaskDay
.
So taskDay[0]
is tmpLIUTaskDay
.
[taskDay[0] datedTask]
is the task array.
[taskDay[0] datedTask][0]
is the tmpLIUDatedTask
.
NSLog(@"%@", [[taskDay[0] datedTask][0] title])
is what you want.
You may use generic type for the NSArray to help you. Something like NSMutableArray<LIUDatedTask *>
.
And you may want to follow some naming convention.
Upvotes: 1