无法在数组中对我的对象进行NSLog

时间:2016-01-10 12:57:12

标签: objective-c arrays

我有一个数组(taskDays),我已经存储在类
中 其中包含一个数组(datedTask)和一个 NSdate(截止日期),在类数组中我存储另一个类(datedTask obj),其中包含一个
NSString(标题)和NSDate(duedate)。
当我尝试从主要的NSLog截止日期工作,但当我尝试NSLog时 标题或duedate xcode崩溃 我已经包含了一张图片,所以你们可以查看一下 如果你想我可以在某处上传我的代码,因为我真的不知道 这里包含哪些代码。

提前致谢!

diagram

包含NSLog代码,因为我认为它可能是那里的问题:

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;
}

以下是崩溃日志:

2016-01-10 13:26:41.749 chrjo564_Ny [1072:41244] - [LIUTaskDay title]:无法识别的选择器发送到实例0x100603220
2016-01-10 13:26:41.751 chrjo564_Ny [1072:41244] *由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; - [LIUTaskDay标题]:无法识别的选择器已发送例如0x100603220&#39;
*
首先抛出调用堆栈:

    0 CoreFoundation 0x00007fff8bd2003c exceptionPreprocess + 172
    1 libobjc.A.dylib 0x00007fff8a2fe76e objc_exception_throw + 43
    2 CoreFoundation 0x00007fff8bd230ad - [NSObject(NSObject)doesNotRecognizeSelector:] + 205
    3 CoreFoundation 0x00007fff8bc68e24 ___转发_
+ 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:以NSException类型的未捕获异常终止 (lldb)

2 个答案:

答案 0 :(得分:1)

taskDayLIUTaskDay类的数组。

您的LIUDatedTask存储在_datedTask的{​​{1}}数组中。

要访问LIUTaskDay对象,您需要调用LIUDatedTask datedTask方法。

所以LIUTaskDaytaskDay[0]

tmpLIUTaskDay是任务数组。

[taskDay[0] datedTask][taskDay[0] datedTask][0]

tmpLIUDatedTask就是你想要的。

您可以使用NSArray的通用类型来帮助您。类似于NSLog(@"%@", [[taskDay[0] datedTask][0] title])

您可能希望遵循一些命名惯例。

答案 1 :(得分:0)

NSLog(@"%@", [taskDay[0][0] title]);