简体   繁体   English

在Objective-C中将对象添加到NSMutableArray

[英]Adding objects to an NSMutableArray in Objective-C

I'm having trouble with what must be a fairly simple task: adding objects to an NSMutableArray in Objective-C. 我遇到了必须是相当简单的任务:将对象添加到Objective-C中的NSMutableArray Here are the million ways I have tried already: 这是我已经尝试过的数百万种方法:

NSMutableArray* foregroundPoints;

Point point;

// Fails with "No viable conversion from 'Point' to 'id _Nonnull'"
[foregroundPoints addObject: point];

// Fails with "Cannot initialise a parameter of type 'id _Nonull' with an rvalue of type 'Point *'"
[foregroundPoints addObject: &point];

// Fails with: "Illegal type 'Point' used in boxed expression"
[foregroundPoints addObject: @(point)];

Point *pointPtr;

// Fails with "Cannot initialise a parameter of type 'id _Nonull' with an lvalue of type 'Point *'"
[foregroundPoints addObject: pointPtr];

// Fails with "Cannot initialise a parameter of type 'id _Nonull' with an rvalue of type 'Point **'"
[foregroundPoints addObject: &pointPtr];

//Fails with: "Illegal type 'Point *' used in boxed expression"
[foregroundPoints addObject: @(pointPtr)];

What should I be doing to add the Point to my NSMutableArray ? 我应该怎么做才能将Point添加到我的NSMutableArray

(NB From the comments and some of the answers I see that I was confused about Point . I'd assumed it was an Objective-C library class but in fact it was a C++ struct picked up from elsewhere in my project. So my question really boils down to this: how do I add a CGPoint to an NSMutableArray ? I'll leave the main question unedited as the discussion in the comments and the answers that don't conflate Point and CGPoint are also interesting.) (注:从评论和一些答案中,我发现我对Point感到困惑。我以为这是一个Objective-C库类,但实际上,这是从我项目的其他地方获得的C ++结构。)真正归结为:我如何将CGPoint添加到NSMutableArray ?我将不编辑主要问题,因为评论中的讨论以及不将PointCGPoint的答案也很有趣。)

As others noted NSArray only holds Objective-C objects. 正如其他人指出的那样,NSArray仅保存Objective-C对象。 To hold C types you need to box them in objects. 要保存C类型,您需要将它们装在对象中。

You need to use NSValue or NSString here. 您需要在此处使用NSValue或NSString。 NSValue has boxing methods for most common Foundation structs and primitives. NSValue具有针对最常见的Foundation结构和基元的装箱方法。

There are also functions that also convert to and from NSString for several of these. 对于其中的一些,还有一些函数也可以与NSString进行相互转换。 See the Foundation Functions Reference. 请参阅《基础功能参考》。

Scalar C types can be boxed and unboxed using the NSValue subclass NSNumber 标量C类型可以使用NSValue子类NSNumber进行装箱和拆箱

nil has to be represented using NSNull nil必须使用NSNull表示

The issue is that only objects can be added to collections such as NSArray or NSDictionary. 问题在于只能将对象添加到诸如NSArray或NSDictionary之类的集合中。

Convert your "point" (likely a CGPoint or NSPoint struct ) into an NSValue object that can be added to the array. 将您的“点”(可能是CGPoint或NSPoint struct )转换为可以添加到数组中的NSValue对象。

Use this class to work with such data types in collections (such as NSArray and NSSet), key-value coding, and other APIs that require Objective-C objects. 使用此类可处理集合中的此类数据类型(例如NSArray和NSSet),键值编码以及其他需要Objective-C对象的API。

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/ https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/

For example, use + valueWithCGPoint: to convert the point to an NSValue representation that can be added to the array. 例如,使用+ valueWithCGPoint:将点转换为可以添加到数组的NSValue表示形式。

CGPoint point;
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[foregroundPoints addObject:pointValue];

Then, later, use - CGPointValue to convert the object back to the original type. 然后,稍后使用- CGPointValue将对象转换回原始类型。

@pkamb answer is the way. @pkamb的答案是这样。

However, if you need performance, use a standard C array to store your points rather than do multiple calls to valueWithCGPoint and CGPointValue. 但是,如果需要性能,请使用标准C数组存储点,而不要多次调用valueWithCGPoint和CGPointValue。

Embed it in a NSObject ( named PointArray, for example ) to do the manipulation. 将其嵌入NSObject(例如,命名为PointArray)中进行操作。

@interface PointArray : NSObject

-(Point)pointAtIndex:(NSUInteger)index;
-(void)addPoint:(Point)point;

…

@property(nonatomic,readonly) NSUInteger numberOfPoints;

@end

@implementation PointArray
{
    Point     *points;
    NSUInteger numberOfPoints;
}

// You'll have to work a bit there ..

@end

You are trying to initialise NSMutableArray object with NSArray. 您正在尝试使用NSArray初始化NSMutableArray对象。

why don't you try this... 你为什么不试试这个...

foregroundPoints = [NSMutableArray arrayWithObjects: @(startPoint), @(endPoint), nil];

Initialise NSMutableArray foregroundPoints to NSMutableArray not with NSArray. 不使用NSArray初始化NSMutableArray前景色点到NSMutableArray。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM