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 *'.
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:
// 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
). 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:
- (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:
// 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.