从 'NSMutableArray *' 分配给 'NSString *' 的不兼容指针类型

Incompatible pointer types assigning to 'NSString *' from 'NSMutableArray *'

我目前正在参加 iOS 开发课程,我在课程中遇到的这行代码会生成此警告:Incompatible pointer types assigning to 'NSString *' from 'NSMutableArray *'.

media.comments = randomComments;

数组是在这一行创建的:

NSMutableArray *randomComments = [NSMutableArray array];

我尝试将 mutableCopy 添加到 randomComments 数组的末尾以消除警告,正如本网站其他地方所建议的那样,但警告仍然存在:

// Doesn't silence warning    
NSMutableArray *randomComments = [[NSMutableArray array]mutableCopy];

有没有人有其他建议如何消除我遇到的不兼容指针类型警告?

完整方法如下:

- (void) addRandomData {
    NSMutableArray *randomMediaItems = [NSMutableArray array];

    for (int i = 1; i <= 10; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];

        if (image) {
            BLCMedia *media = [[BLCMedia alloc] init];
            media.user = [self randomUser];
            media.image = image;

            NSUInteger commentCount = arc4random_uniform(10);
            NSMutableArray *randomComments = [NSMutableArray array];

            for (int i  = 0; i <= commentCount; i++) {
                BLCComment *randomComment = [self randomComment];
                [randomComments addObject:randomComment];
            }

            media.comments = randomComments;

            [randomMediaItems addObject:media];
        }
    }

    self.mediaItems = randomMediaItems;
}

您的基本问题是尝试将一种对象类型 (NSMutableArray) 分配给另一种对象类型 (NSString)。您需要确定哪种类型是正确的,并将两个对象都设为该类型。

假设您正在创建一个对象数组 (BLCMedia),每个对象都有一个 comments 属性,类型为 NSString,下面应该修复你的问题:

- (void) addRandomData {
    NSMutableArray *randomMediaItems       = [NSMutableArray array];
    for (int i = 1; i < 11; i = i + 1) {
        NSString *imageName                = [NSString stringWithFormat:@"%d.jpg", i];
        UIImage *image                     = [UIImage imageNamed:imageName];
        if (image != nil) {
            BLCMedia *media                = [[BLCMedia alloc] init];
            media.user                     = [self randomUser];
            media.image                    = image;
            NSUInteger commentCount        = arc4random_uniform(10);
            for (int i = 0; i < commentCount; i = i + 1) {
                BLCComment *randomComment  = [self randomComment];
                media.comments             = randomComment;
            }
            [randomMediaItems addObject:media];
        }
    }
    self.mediaItems = randomMediaItems;
}

但是,如果 BLCMedia 对象 打算将 NSString 对象数组作为其 comments 属性,以下将解决您的问题:

// In BLCMedia.h/.m
// Change this:
@property (copy  , nonatomic) NSString       *randomComments;
// To this:
@property (strong, nonatomic) NSMutableArray *randomComments;


- (void) addRandomData {
    NSMutableArray *randomMediaItems           = [NSMutableArray array];
    for (int i = 1; i < 11; i = i + 1) {
        NSString *imageName                    = [NSString stringWithFormat:@"%d.jpg", i];
        UIImage *image                         = [UIImage imageNamed:imageName];
        if (image != nil) {
            BLCMedia *media                    = [[BLCMedia alloc] init];
            media.user                         = [self randomUser];
            media.image                        = image;
            NSUInteger commentCount            = arc4random_uniform(10);
            NSMutableArray *randomComments     = [NSMutableArray array];
            for (int i = 0; i <= commentCount; i = i + 1) {
                BLCComment *randomComment      = [self randomComment];
                [randomComments addObject:randomComment];
            }
            media.comments = randomComments;
            [randomMediaItems addObject:media];
        }
    }
    self.mediaItems = randomMediaItems;
}