将 int 赋值给新 int

发布于 2024-11-10 08:32:27 字数 727 浏览 2 评论 0原文

到目前为止,我有代码

- (void)CreatenewBlock:(int)blockCountToSpawn
{
    for (int i=0; i != blockCountToSpawn; i++) 
    {
        theBlockxCord = arc4random() % 4;
        theBlockyCord = arc4random() % 4;
        NSLog(@"New Block with Cords (%i, %i)",theBlockxCord, theBlockyCord);
    }

}

This 循环直到达到 blockCountToSpawn 的数量 这完全按照我想要的方式工作,但我想每次将 theBlockxCord 设置为一个新变量。所以最终结果会是这样的:

theBlockxCordOfBlock1=2
theBlockxCordOfBlock2=4
theBlockxCordOfBlock3=1
theBlockxCordOfBlock4=3

而不是每次都覆盖 theBlockXCord。

对于奖励积分,是否有一种方法可以在数组中调用它们,这样我就不必继续这样做:

if (theBlockxCordOfBlock1 == 2 || theBlockxCordOfBlock3 == 2 ..etc)
{
   do stuff..
}

So far I have the code

- (void)CreatenewBlock:(int)blockCountToSpawn
{
    for (int i=0; i != blockCountToSpawn; i++) 
    {
        theBlockxCord = arc4random() % 4;
        theBlockyCord = arc4random() % 4;
        NSLog(@"New Block with Cords (%i, %i)",theBlockxCord, theBlockyCord);
    }

}

This loops until it reaches the anount of blockCountToSpawn
This works exactly how I want it to but I want to set theBlockxCord to a new variable each time. So the end result would be something like:

theBlockxCordOfBlock1=2
theBlockxCordOfBlock2=4
theBlockxCordOfBlock3=1
theBlockxCordOfBlock4=3

Instead of theBlockXCord being overwritten each time.

For bonus points, Is there a single way to call of them maybe in an array so I don't have to keep doing this:

if (theBlockxCordOfBlock1 == 2 || theBlockxCordOfBlock3 == 2 ..etc)
{
   do stuff..
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无可置疑 2024-11-17 08:32:27

您可以使用 C 数组或 NSMutableArray。您必须先将 int 转换为 NSNumber,然后才能将其添加到数组中。

- (void)CreatenewBlock:(int)blockCountToSpawn
{
    NSMutableArray *blockXCoord = [NSMutableArray array]; // Retain it as needed.
    NSMutableArray *blockYCoord = [NSMutableArray array];
    for (int i=0; i != blockCountToSpawn; i++) 
    {
        [blockXCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
        [blockYCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
    }

    ...
}

如果您想搜索2,请执行此操作

if ( [blockXCoord indexOfObject:[NSNumber numberWithInt:2]] != NSNotFound ) {
    ... do stuff
}

if ( [blockXCoord containsObject:[NSNumber numberWithInt:2]] ) {
    ... do stuff
}

编辑

for ( int i = 0; i < [blockXCoord count]; i++ ) {
    NSPoint point = NSMakePoint([[blockXCoord objectAtIndex:i] intValue],[[blockYCoord objectAtIndex:i] intValue]);

    ... do something with the point.
}

You can use C arrays or an NSMutableArray. You will have to convert the ints to NSNumbers before you can add it to the array.

- (void)CreatenewBlock:(int)blockCountToSpawn
{
    NSMutableArray *blockXCoord = [NSMutableArray array]; // Retain it as needed.
    NSMutableArray *blockYCoord = [NSMutableArray array];
    for (int i=0; i != blockCountToSpawn; i++) 
    {
        [blockXCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
        [blockYCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
    }

    ...
}

if you want to search for 2 then do this

if ( [blockXCoord indexOfObject:[NSNumber numberWithInt:2]] != NSNotFound ) {
    ... do stuff
}

or

if ( [blockXCoord containsObject:[NSNumber numberWithInt:2]] ) {
    ... do stuff
}

EDIT

for ( int i = 0; i < [blockXCoord count]; i++ ) {
    NSPoint point = NSMakePoint([[blockXCoord objectAtIndex:i] intValue],[[blockYCoord objectAtIndex:i] intValue]);

    ... do something with the point.
}
浅笑轻吟梦一曲 2024-11-17 08:32:27

对于第一个问题:将结果放入传统数组(就像任何 C 程序一样)或 NSMutableArray 中。 http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

对于您的第二个问题:如果您使用 NSMutableArray 来存储对象(例如 NSNumber),您可以调用 containsObject: 来确定对象是否在数组中。 http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject

For your first question: Put your results in either a conventional array just like any C program or in an NSMutableArray. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

For your second question: If you used an NSMutableArray to store objects (say, an NSNumber), you can call containsObject: to determine if an object is in the array. http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文