在 switch 语句中混合 Objective C 和 C 尝试添加子视图 iphone SDK
我遇到了一个奇怪的问题。我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你想做什么,但可能你有点混淆你的类型:
for(
之后的self.composizione
是多余的,它不会做任何事情吗?*composizione
是composizione
指向的值,它应该是一个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:
self.composizione
afterfor(
is redundant, it doesn't do anything. Should it?*composizione
is the value pointed to bycomposizione
, which should be anint*
(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 ofcomposizione++
.