【问题标题】:Trouble after adding an NSArray of images添加图像的 NSArray 后出现问题
【发布时间】:2014-07-21 14:10:25
【问题描述】:

我的应用程序有点问题,对这种事情不熟悉,我有点难以弄清楚发生了什么。

我遇到的错误如下

  • 线程 1:EXC_BAD_ACCESS(代码=exc_I386_GPFLT)

  • 隐式转换丢失整数精度:“NSUInterger”(又名 unsigned long)到“u_int32”(又名“unsigned int”)**已解决*

第一个不显示为红色或黄色错误,它只是我的部分代码下的贪婪文本,在下面的“图 1”中显示

第二部分是数组中的黄色错误,用于选择要显示在UIImageView 中的随机图像(图2)

图 1

 NSArray *imageNameArray = [[NSArray alloc] initWithObjects:@"toppipestyleone.png", "toppipestyletwo.png", "toppipestylethree.png", "toppipestylefour.png", "toppipestylefive.png", nil];

这是我放置UIImageView 的方法我在这个方法中也有代码告诉UIImageView 从屏幕的右到左滚动,我将在“图3”中发布我的整个方法

图2

PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform([imageNameArray count])]];

图 3

-(void)PlacePipe{



    NSArray *imageNameArray = [[NSArray alloc] initWithObjects:@"toppipestyleone.png", "toppipestyletwo.png", "toppipestylethree.png", "toppipestylefour.png", "toppipestylefive.png", nil];

    PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform([imageNameArray count])]];


    RandomTopPipePosition = arc4random() %350;
    RandomTopPipePosition = RandomTopPipePosition - 228;
    RandomBottomPipePosition = RandomTopPipePosition + 660;

    PipeTop.center = CGPointMake(340-10, RandomTopPipePosition);
    randomImagebottom.center = CGPointMake(340-10, RandomBottomPipePosition);


}

我认为第二个错误与 32 位和 64 位设备有关,但我找不到针对我的确切问题的实际解决方案,我读到的大多数问题都是关于人们使用 NSZombies?我不太确定那是什么。

【问题讨论】:

    标签: ios objective-c uiimageview nsarray


    【解决方案1】:

    1

    !真正的错误:来自 kirsteins: "只有第一个数组对象是 NSString @"toppipestyleone.png",其他都是 c 字符串字面量。你应该在它们前面加上 @ 来组成 NSString 字面量。"

    他/她删除了他们的答案,尽管它是正确的......

    NSArray *imageNameArray = [[NSArray alloc] initWithObjects:@"toppipestyleone.png", @"toppipestyletwo.png", @"toppipestylethree.png", @"toppipestylefour.png", @"toppipestylefive.png", nil];
    

    2

    数组从零开始,count-1 是最后一个索引。关于警告。投射它:

    所以:

    NSUInteger index = (NSUInteger)arc4random_uniform((int)[imageNameArray count]-1);
    PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:index]];
    

    【讨论】:

      【解决方案2】:

      我认为在选择随机数时,您的数组计数会变高,请使用它来获取数组计数之间的随机索引..

      int r = arc4random() % [yourArray count];
         if(r<[yourArray count])
          obj=[yourArray objectAtIndex:r];
         else
             {
           //error message
              }
      

      【讨论】:

      • 你是对的,但为什么不告诉 arc4random_uniform max 是 count-1?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多