[__NSArrayI 长度]:在视图中移动时发送到实例的无法识别的选择器

[__NSArrayI length]: unrecognized selector sent to instance when moving through views

当我在通向详细视图的主视图中单击标签时遇到了这个错误(它应该显示在 ReasonLibrary 的数据模型中显示的标签。

我的代码:

主视图控制器header

@interface MasterViewController : UIViewController

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *reasonLabelViews;

@end

主视图控制器实现

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "Reasons.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    for (NSUInteger index = 0; index < self.reasonLabelViews.count; index++) {

        Reasons *reason = [[Reasons alloc] initWithIndex:index];

        UILabel *reasonLabelView = self.reasonLabelViews[index];

        reasonLabelView.text = reason.reasonsRazao;

    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    UILabel *reasonLabelView = (UILabel *)[sender view];

    if ([self.reasonLabelViews containsObject:reasonLabelView]) {
        NSUInteger index = [self.reasonLabelViews indexOfObject:reasonLabelView];

        DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;

        detailViewController.reason = [[Reasons alloc] initWithIndex:index];
    }
}

- (IBAction)showReasonDetail:(id)sender {
    [self performSegueWithIdentifier:@"showReasonDetail" sender:sender];
}



@end

详细视图控制器header

#import <UIKit/UIKit.h>

@class Reasons;

@interface DetailViewController : UIViewController

@property (strong, nonatomic) Reasons *reason;

@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;

@property (weak, nonatomic) IBOutlet UILabel *motiveLabel;

@property (weak, nonatomic) IBOutlet UILabel *zeroFiveLabel;

@end

详细视图控制器实现

#import "DetailViewController.h"
#import "Reasons.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.reason) {
        self.reasonLabel.text = self.reason.reasonsRazao;
        self.motiveLabel.text = self.reason.randomFact;
        self.zeroFiveLabel.text = self.reason.reasonsDeZeroACinco;
    }


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

原因库header

#import <Foundation/Foundation.h>


extern NSString *const kRazao;
extern NSString *const kMotivo;
extern NSString *const kDeZeroACinco;



@interface ReasonLibrary : NSObject

@property (strong,nonatomic) NSArray *library;

- (NSString *)randomFact;

@end

ReasonsLibrary 实现

#import "ReasonLibrary.h"

@implementation ReasonLibrary

NSString *const kRazao = @"razao";
NSString *const kMotivo = @"motivo";
NSString *const kDeZeroACinco = @"dezeroacinco";

- (instancetype)init
{
    self = [super init];
    if (self) {
        _library = @[@{kRazao: @"Razao 1",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"1",
                       },
                     @{kRazao: @"Razao 2",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"2",
                       },
                     @{kRazao: @"Razao 3",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"3",
                       },
                     @{kRazao: @"Razao 4",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"4",
                       },
                     @{kRazao: @"Razao 5",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"5",
                       },
                     @{kRazao: @"Razao 6",
                       kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
                       kDeZeroACinco: @"6",
                       }

                     ];
    }
    return self;
}

- (NSString *)randomFact {
int random = arc4random_uniform((int)self.library.count);
return [self.library objectAtIndex:random];

} @结束

原因header

#import <Foundation/Foundation.h>

@interface Reasons : NSObject

@property (strong, nonatomic) NSString *reasonsRazao;
@property (strong, nonatomic) NSString *reasonsMotivo;
@property (strong, nonatomic) NSString *reasonsDeZeroACinco;
@property (strong, nonatomic) NSString *randomFact;

-(instancetype)initWithIndex:(NSUInteger)index;

@end

实施原因

#import "Reasons.h"
#import "ReasonLibrary.h"

@implementation Reasons

-(instancetype)initWithIndex:(NSUInteger)index {

    self = [super init];

    if (self) {

    ReasonLibrary *reasonlibrary = [[ReasonLibrary alloc] init];
    NSArray *library = reasonlibrary.library;

    NSDictionary *reasonsDictionary = library[index];

    _reasonsRazao = [reasonsDictionary objectForKey:kRazao];
    _reasonsMotivo = [reasonsDictionary objectForKey:kMotivo];
    _reasonsDeZeroACinco = [ reasonsDictionary objectForKey:kDeZeroACinco];

    }
    return self;
}

@end

kMotivo 的对象在您的代码中是 NSArray。您将其分配给 NSString 属性,然后分配给 UILabel 文本 属性。 UILabel 尝试在 NSArray 上调用 length 并因异常而崩溃。

像下面这样的东西会起作用:

NSArray *motives = [reasonsDictionary objectForKey:kMotivo];
if (motives != nil && motives.count > 0) {
  int random = arc4random_uniform(motives.count);
  _reasonsMotivo = motives[random];
}

上面的代码将从数组中获取第一个可用的动机并将其分配给 _reasonsMotivo。它应该被插入而不是下面的代码行:

_reasonsMotivo = [reasonsDictionary objectForKey:kMotivo];

而不是:

kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"]

使用

kMotivo: [[NSArray alloc]initWithObjects:@"Chapter 1",@"Chapter 2",@"Chapter 3",@"Chapter 4",@"Chapter 5",nil]