简体   繁体   English

为什么我从Xcode收到此警告? iOS代码似乎很好

[英]Why am I getting this warning from Xcode? The iOS code seems fine

This one has me stumped. 这个让我难过。 I'm writing an iPhone app that tracks bus schedules. 我正在写一个跟踪公交时刻表的iPhone应用程序。 Users can bookmark their favorite bus stops so they can jump directly to them from the home screen. 用户可以为他们喜爱的公交车站添加书签,以便他们可以从主屏幕直接跳转到他们。 I manage the list of favorites in my AppDelegate class (unrelated code has been redacted): 我在AppDelegate类中管理收藏夹列表(不相关的代码已被编辑):

@interface AppDelegate : NSObject <UIApplicationDelegate>
+ (BOOL) isInFavorites: (FavoriteStopData*) inStop;
@end

I have a view controller that presents the list of stops for a given bus route and lets users select one to see predicted bus arrival times for that stop in a new view (and maybe add the stop to their list of favorites): 我有一个视图控制器,它显示给定公交路线的停靠点列表,并允许用户选择一个在新视图中查看该站点的预测公交车到达时间(并可能将停靠点添加到他们的收藏夹列表中):

@implementation RouteStopsViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FavoriteStopData *stopData = [[FavoriteStopData alloc] init];
// ... set various properties in stopData from data in the selected cell
    FavoriteStopViewController *fvc = [[FavoriteStopViewController alloc] initWithNibName:@"FavoriteStopViewController" bundle:nil];
    fvc.stop = stopData;
    fvc.isBookmarked = [AppDelegate isInFavorites:stopData];
    [stopData release];
    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}
@end

The line 这条线

fvc.isBookmarked = [AppDelegate isInFavorites:stopData];

gets two warnings: 得到两个警告:

"'AppDelegate' may not respond to +isInFavorites:" “'AppDelegate'可能无法回复+ isInFavorites:”
"Passing argument 1 of 'setIsBookmarked' makes integer from pointer without a cast" “传递'setIsBookmarked'的参数1从指针中生成整数,而不使用强制转换”

I can't see any reason for Xcode to think '+isInFavorites:' is undefined, yet it does. 我看不出Xcode有什么理由认为'+ isInFavorites:'是未定义的,但确实如此。 I've verified that these possible causes for the warning are not in fact the case: 我已经确认这些警告的可能原因实际上并非如此:

  • '+isInFavorites:' is declared in "AppDelegate.h" (as shown above) '+ isFavorites:'在“AppDelegate.h”中声明(如上所示)

  • "RouteStopsViewController.m" does #import "AppDelegate.h" (and "FavoriteStopData.h" and "FavoriteStopViewController.h") “RouteStopsViewController.m”执行#import“AppDelegate.h”(以及“FavoriteStopData.h”和“FavoriteStopViewController.h”)

  • 'isBookmarked' is a public BOOL property on FavoriteStopViewController 'isBookmarked'是FavoriteStopViewController上的公共BOOL属性

  • The code is not being munged by some #define macro; 一些#define宏没有使用代码; when I preprocess "RouteStopsViewController.m", this code is unchanged. 当我预处理“RouteStopsViewController.m”时,此代码保持不变。

The code behaves correctly, but I REALLY don't want to live with a warning that I must ignore every time the code compiles, and disabling this warning with some #pragma is a road I'd rather not take unless I have to. 代码表现正常,但我真的不想忍受每次代码编译时都必须忽略的警告,并且使用#pragma禁用此警告是一条我不愿意接受的道路,除非我必须这样做。

I've tried renaming the method name, the variable names, using the method to set a local BOOL variable and then setting the property with that, using a conditional operator (x ? y : z) to make sure I'm passing a BOOL to the property ... nothing works. 我已经尝试重命名方法名称,变量名称,使用方法设置本地BOOL变量,然后使用条件运算符(x?y:z)设置属性,以确保我传递BOOL到财产......没有任何作用。 That first warning never goes away. 第一次警告永远不会消失。

Can anyone suggest why Xcode is giving me this warning? 任何人都可以建议为什么Xcode会给我这个警告?

This is with Xcode 4.2 (Build 4C199) and iOS 5 SDK running in the 5.0 iPhone Simulator on a MacBook Pro running Mac OS X 10.6.8 (Snow Leopard). 这是在运行Mac OS X 10.6.8(Snow Leopard)的MacBook Pro上的5.0 iPhone模拟器中运行的Xcode 4.2(Build 4C199)和iOS 5 SDK。

If the +isInFavorites: method was completely unknown to the compiler then you'd see a warning like +isInFavorites: not found (return type defaults to 'id') . 如果编译器完全不知道+isInFavorites:方法,那么你会看到类似+isInFavorites: not found (return type defaults to 'id')的警告+isInFavorites: not found (return type defaults to 'id')

If you're not seeing that warning then we can assume that the compiler has seen a declaration of that method somewhere . 如果您没有看到该警告,那么我们可以假设编译器已经在某处看到了该方法的声明。 However, the compiler expects this method to return a pointer rather than a BOOL, which is why you're seeing the makes integer from pointer without a cast warning. 但是,编译器期望此方法返回指针而不是BOOL,这就是为什么你在makes integer from pointer without a cast警告的makes integer from pointer without a cast看到makes integer from pointer without a cast原因。

Check for any other declarations of an isInFavorites: method in your project. 检查项目中isInFavorites:方法的任何其他声明。 Check for any global variables named AppDelegate that may conflict with your class name. 检查可能与您的类名冲突的任何名为AppDelegate全局变量。 Check for any circular imports between AppDelegate.h and RouteStopsViewController.h. 检查AppDelegate.h和RouteStopsViewController.h之间的任何循环导入。 Try renaming your AppDelegate class. 尝试重命名您的AppDelegate类。

在你的appdelegate.h文件中声明你的isInFavorites方法。

'AppDelegate' may not respond to +isInFavorites: 'AppDelegate'可能无法回复+ isInFavorites:
Passing argument 1 of 'setIsBookmarked' makes integer from pointer without a cast 传递'setIsBookmarked'的参数1会使指针中的整数不带强制转换

The first error causes the second. 第一个错误导致第二个错误。 The compiler's confusion on the existence of +isInFavorites: causes the compiler to assume the return type is id 编译器对+isInFavorites:存在的+isInFavorites:导致编译器假定返回类型为id

This assumption causes the warning of making integer from pointer without a cast 这个假设导致警告在没有强制转换的情况下从指针生成整数

You really have to focus on the first warning. 你真的必须专注于第一个警告。

Are these the only warnings? 这些是唯一的警告吗?

Try changing your AppDelegate.h to 尝试将AppDelegate.h更改为

@class FavoriteStopData
@interface AppDelegate : NSObject <UIApplicationDelegate>
+ (BOOL) isInFavorites: (FavoriteStopData*) inStop;
@end

If you still have issues, you might want to consider making this an instance method instead, considering the appDelegate is valid the whole runtime of your app 如果您仍然遇到问题,您可能需要考虑将此设置为实例方法,考虑到appDelegate对应用程序的整个运行时有效

Found it. 找到了。 Even makes perfect sense ... now that I know exactly where to look. 即使是完美的感觉......现在我确切地知道要去哪里看。

My project has two targets: Dev for the version I use to test new code, and App for the user-facing version. 我的项目有两个目标:Dev用于测试新代码的版本,以及用于面向用户版本的App。 Each has its own AppDelegate class (and some other duplicates). 每个都有自己的AppDelegate类(以及其他一些重复项)。 Code specific to one target or the other goes into either the ./Dev/ or the ./App/ folder. 特定于一个目标或另一个目标的代码进入./Dev/或./App/文件夹。 Common code goes into other folders. 常用代码进入其他文件夹。

Recently I promoted one Dev-specific class to be used in both targets ... but hadn't yet moved the files out of the Dev folder. 最近我推出了一个特定于Dev的类,用于两个目标......但还没有将文件移出Dev文件夹。 This was my problematic RouteStopsViewController. 这是我有问题的RouteStopsViewController。 My project was compiling the right "AppDelegate.m", but Xcode was finding the 'wrong' (to my thinking) "AppDelegate.h" because it was looking first in the same folder as "RouteStopsViewController.m". 我的项目正在编译正确的“AppDelegate.m”,但Xcode发现'错误'(我的想法)“AppDelegate.h”,因为它首先在与“RouteStopsViewController.m”相同的文件夹中查找。

The fix was easy: move RouteStopsViewController out of the Dev-specific folder into one for code shared by both targets. 修复很简单:将RouteStopsViewController从特定于Dev的文件夹中移动到一个用于两个目标共享的代码。 Now Xcode uses the "AppDelegate.m" file it's compiling to find the matching "AppDelegate.h". 现在,Xcode使用它正在编译的“AppDelegate.m”文件来查找匹配的“AppDelegate.h”。

I knew at the time I should move that RouteStopsViewController class when I decided to reuse it in the App target, I just didn't get around to it. 我知道当我决定在App目标中重用它时,我应该移动那个RouteStopsViewController类,我只是没有解决它。 When it comes to writing code, trust your nose. 在编写代码时,要相信自己的鼻子。 If it smells funny, it very probably is. 如果它闻起来很有趣,很可能就是这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在Xcode for iOS中得到了“枚举类型的隐式转换”警告,我不知道为什么 - I am getting an “Implicit conversion from enumeration type” warning in Xcode for iOS and I don't know why 为什么我在XCode中收到过时的警告 - Why am I getting a deprecated warning in XCode iOS:为什么我在Xcode中得到“未使用的变量”? - iOS : Why am I getting 'Unused variable' in Xcode? 为什么在调试过程中会收到有关从远程iOS设备读取Xcode框架文件的警告? - Why am I getting warnings about Xcode reading framework files from the remote iOS device during debugging? 为什么我在Xcode中遇到此Parse问题? - Why am I getting this Parse Issue in Xcode? 为什么我收到来自此请求iOS JSON的空响应 - Why am I getting Empty Response from this Request iOS JSON 为什么在以下实例方法上收到“冲突类型”警告? - Why am I getting a “Conflicting types” warning on the below instance method? 为什么我收到有关我的应用程序委托和CLLocationManageDelegate的警告? - Why am I getting this warning about my app delegate and CLLocationManageDelegate? 当我从经度和纬度创建位置字符串时,为什么我在iOS中收到此链接器错误? - Why am I getting this linker error in iOS when I create location strings from longitude and latitude? 为什么我收到警告“找到名为“ center”的多个方法” - Why am I am getting the warning “Multiple methods named 'center' found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM