【问题标题】:[__NSArrayI length]: unrecognized selector sent to instance when moving through views [duplicate][__NSArrayI length]: unrecognized selector sent to instance when moving through views [duplicate]
【发布时间】:2015-09-19 08:43:21
【问题描述】:

我在单击主视图上的标签时遇到此错误,该标签通向详细视图(该标签应显示为 ReasonLibrary 的数据模型中显示的标签。

我的代码:

主视图控制器标题

@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

详细视图控制器标题

#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

原因库标题

#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

原因库实现

#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];

} @结束

原因标题

#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

【问题讨论】:

标签: ios objective-c segue unrecognized-selector


【解决方案1】:

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 的对象更改为 NSArray,但我得到:“使用 NSString 类型的表达式初始化 'NSArray *' 的不兼容指针类型
  • 我不太了解您的应用程序的逻辑。 kMotivo 是许多字符串。您想将哪些字符串分配给 _reasonsMotivo ?
  • @user1370624 我更新了你需要使用的代码替换
  • 我应该在哪里插入这个?
  • @user1370624 插入此代码块而不是您的代码行 _reasonsMotivo = [reasonsDictionary objectForKey:kMotivo];
【解决方案2】:

代替:

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]

【讨论】:

  • 为什么你认为它会有所帮助?数组用文字正确初始化。
  • 我用过它,它以前工作过。有帮助吗?如果是,我可以投票吗?
  • 好吧,我换个方式问。为什么数组的字面初始化比您提出的旧式数组初始化更好?
  • 由于某种原因,当我传递一个未像这样初始化的数组时,它会抛出不兼容错误,因为数组只是一个指针,而 NSArray 或 NSMutableArray 是对象,它期望和对象。跨度>
  • 您混淆了 C 静态数组和数组的 ObjC 文字。每次看到 @ 时,它都意味着它是用于定义某些特定对象的字面量(语法糖化的快捷方式)。虽然像 {0, 1, 2} 这样的 C 数组肯定不是 NSArray 对象并且会导致问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多