gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:42:19 24 4
gpt4 key购买 nike

我目前正在上一个iOS开发 class ,并且在 class 中遇到的这一行代码会产生以下警告:不兼容的指针类型从'NSMutableArray *'分配给'NSString *'。

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

关于ios - 不兼容的指针类型从'NSMutableArray *'分配给'NSString *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28322106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com