gpt4 book ai didi

objective-c - 如何在Objective-C中解析对象数组?

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

来自C++,这是我的问题:

我已经创建了这种类型的对象:

    Size *one = [[Size alloc] initWithX: 3 andY: 1];
Size *two = [[Size alloc] initWithX: 4 andY: 7];
// etc...
Size *thirtythree = [[Size alloc] initWithX: 5 andY: 9];

(每个对象都有 @property int x;@property int y;。)

我已经存储在数组中,如下所示:
NSArray *arrayOfSizes;

arrayOfSizes = [NSArray arrayWithObjects:one,two,three,four,five,six,
seven,eight,nine,ten,eleven,twelve,thirteen,
fourteen,fifteen,sixteen,seventeen,eighteen,
nineteen,twenty,twentyone,twentytwo,
twentythree,twentyfour,twentyfive,twentysix,
twentyseven,twentyeight,twentynine,thirty,
thirtyone,thirtytwo,thirtythree nil];

现在我有一个类型的对象:
Myobject *myObject = [[Myobject alloc] initWithX: 5 andY: 3];

还有一个 @property int x;@property int y; ...

并且我想将其值与在数组中找到的对象的值进行比较,直到找到具有相似值的数组对象。。但是我不知道如何在Obj-C中做到这一点。 (在C++中,我只想将 vector v;v.size();v[x]; ..etc ...一起使用。)

这是我正在寻找的.. :)
while( !wholeOfArrayOfSizesChecked && !found)
{
if ( // x & y of object in array is equal to x & y of myObject )
{
found = YES;
}
else if( // whole of array checked)
{
wholeOfArrayOfSizesChecked = YES;
}
else
{
//move on to the next object of the array..
}
}

在此先感谢您的帮助!

最佳答案

仅使用您给定的结构。虽然有更聪明的方法:)

wholeOfArrayOfSizesChecked = NO; 
int currObj = 0
while( !wholeOfArrayOfSizesChecked && !found)
{
Size *current = (Size *)[arrayOfSizes objectAtIndex:i];
if (myObject.x == current.x && myObject.y == current.y)
{
found = YES;
}
else if(currObj == [arrayOfSizes count] -1 )
{
wholeOfArrayOfSizesChecked = YES;
}
else
{
currObj++;
}
}

关于objective-c - 如何在Objective-C中解析对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885438/

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