在 switch 语句中混合 Objective C 和 C 尝试添加子视图 iphone SDK

发布于 2024-09-11 17:33:16 字数 1138 浏览 9 评论 0原文

我遇到了一个奇怪的问题。我有一个 for 循环,循环内有一个 switch 语句。 for 循环中的计数器是一个指向原始数组开头的指针,它会自行递增,直到找到“终止符编号”。指向的数组用于构建视图,每个元素代表一个要放入视图中的子视图。 这是代码:

    for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
    NSLog(@"%d",c);
    switch (c) {
        case kForchettaPesce:   case kForchettaNormale:
        {
            NSString *imagePath;
            imagePath=[[NSString alloc]initWithFormat:@"%@/%d.png",[[NSBundle mainBundle]resourcePath],c];
            UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
            [imagePath release];
            NSLog(@"pippo");
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
            imageView.image=image;
            [image release];
            [self.view addSubview:imageView];
            [imageView release];

        break;
        }


        default:
        break;
    }
    NSLog(@"%d",(int)*composizione);



}

调试它我发现它工作得很好,直到它尝试添加子视图。它似乎停留在循环中,并且日志在无限循环中始终显示相同的数据。如果我删除 -addSubview 方法,我没有问题,日志语句显示我期望看到的内容。 我做错了什么? 问候, 安德里亚

I'm experiencing a weird problem. I have a for loop, inside the loop there is a switch statement. The counter in the for loop is a pointer that points to the beginning of a primitive array, it increments itself till it finds a "terminator number". The array pointed is used to build a view, each elements represent a subview to put in the view.
Here is the code:

    for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
    NSLog(@"%d",c);
    switch (c) {
        case kForchettaPesce:   case kForchettaNormale:
        {
            NSString *imagePath;
            imagePath=[[NSString alloc]initWithFormat:@"%@/%d.png",[[NSBundle mainBundle]resourcePath],c];
            UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
            [imagePath release];
            NSLog(@"pippo");
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
            imageView.image=image;
            [image release];
            [self.view addSubview:imageView];
            [imageView release];

        break;
        }


        default:
        break;
    }
    NSLog(@"%d",(int)*composizione);



}

Debugging it I've found out that works perfectly untill it tries to add the subview. It seems to stay stucked in the loop and the log shows always the same datas in an infinite cycle. If I delete the -addSubview method I have no problem, the log statement shows what I expect to see.
What am I doing wrong?
Regards,
Andrea

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

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

发布评论

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

评论(1

顾北清歌寒 2024-09-18 17:33:16

我不确定你想做什么,但可能你有点混淆你的类型:

for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
  • for( 之后的 self.composizione 是多余的,它不会做任何事情吗?
  • *composizionecomposizione 指向的值,它应该是一个 int* (是它?)
  • composizione++指针前进到内存中的下一个位置,而不是*composizione

。 :也许您需要 (*composizione)++ 而不是 composizione++

I'm not sure what you are trying to do, but it may be that you're kind of mixing up your types:

for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
  • the self.composizione after for( is redundant, it doesn't do anything. Should it?
  • *composizione is the value pointed to by composizione, which should be an int* (is it?)
  • composizione++ advances the pointer to the next location in memory, not the value of *composizione

in short: maybe you need a (*composizione)++ instead of composizione++.

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