简体   繁体   English

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

[英]Incompatible pointer types assigning to 'NSString *' from 'NSMutableArray *'

I'm presently taking an iOS development course and this line of the code I've encountered in the course generates this warning: Incompatible pointer types assigning to 'NSString *' from 'NSMutableArray *'. 我目前正在上一个iOS开发课程,并且在课程中遇到的这一行代码会产生以下警告:不兼容的指针类型从'NSMutableArray *'分配给'NSString *'。

media.comments = randomComments;

The array is created in this line: 在此行中创建数组:

NSMutableArray *randomComments = [NSMutableArray array];

I tried adding mutableCopy to the end of randomComments array to silence the warning, as suggested elsewhere on this site, but the warning remains: 我尝试将mutableCopy添加到randomComments数组的末尾以使警告静音,如本网站上其他地方所建议的那样,但警告仍然存在:

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

Does anyone have another suggestion how to quell the incompatible pointer type warning I've encountered? 有人有其他建议如何平息我遇到的不兼容的指针类型警告吗?

Here is the complete method: 这是完整的方法:

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

Your basic problem is trying to assign one object type ( NSMutableArray ) to a different object type ( NSString ). 您的基本问题是尝试将一种对象类型( NSMutableArray )分配给另一种对象类型( NSString )。 You need to decide which is the correct type and make both objects that type. 您需要确定哪个是正确的类型,然后使两个对象都键入该类型。

Assuming that you're creating an array of objects ( BLCMedia ) each of which has a comments property, of type NSString , the following should fix your issue: 假设您要创建一个对象数组( BLCMedia ),每个对象都有一个NSString类型的comments属性,则以下内容将解决您的问题:

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

If, however, a BLCMedia object is intended to have an array of NSString objects as its comments property, the following would fix your issue: 但是,如果一个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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:从“ NSArray”分配给“ NSMutableArray *”的指针类型不兼容 <NSString *> * _Nullable&#39; - Warning: Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray<NSString *> * _Nullable' 从'nsarray'分配给'nsmutablearray'的不兼容指针类型 - incompatible pointer types assigning to 'nsmutablearray ' from 'nsarray ' 不兼容的指针类型从NSString分配给“ UITextField” - Incompatible pointer types assigning to “UITextField” from NSString 从iOS7中的NSMutablearray分配给NsObject的不兼容指针类型警告 - incompatible pointer types assigning to NsObject from NSMutablearray in iOS7 warning 如何修复警告:从&#39;NSString *&#39;分配给&#39;NSMutableString *&#39;的指针类型不兼容 - How to fix warning: Incompatible pointer types assigning to 'NSMutableString *' from 'NSString *' 分配给&#39;NSString&#39;到&#39;NSString&#39;的不兼容指针类型 - Incompatible pointer types assigning to 'NSArray' to 'NSString' plist的UICollectionView中的iOS节-不兼容的指针类型从UILabel分配给NSString - iOS Sections in UICollectionView from plist - incompatible pointer types assigning to NSString from UILabel 不兼容的指针类型分配给&#39;UILabel * __ weak&#39;到&#39;NSString * __ strong&#39;? - incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'? 从“ NSData *”分配给“ NSMutableData *”的指针类型不兼容 - Incompatible pointer types assigning to 'NSMutableData *' from 'NSData *' 不兼容的指针类型从“ UITableViewCell”分配给“ TableViewCell” - Incompatible pointer types assigning to 'TableViewCell' from 'UITableViewCell'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM