xcode 编写针对 iOS 6 Base SDK 编译的 iOS7 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18303786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing iOS7 code that compiles against iOS 6 Base SDK
提问by Carlos P
We have an iOS app on-sale now, and we're developing the iOS 7 version on XCode 5 DP using the same code base.
我们现在有一个 iOS 应用程序在售,我们正在使用相同的代码库在 XCode 5 DP 上开发 iOS 7 版本。
We really need to release an update right now for existing iOS 5/6 customers but, of course, when we re-load the project into XCode 4, it complains about non-existing properties since the Base SDK then becomes iOS6, not 7:
我们现在真的需要为现有的 iOS 5/6 客户发布更新,但是,当然,当我们将项目重新加载到 XCode 4 时,它会抱怨不存在的属性,因为 Base SDK 变成了 iOS6,而不是 7:
// Only run this bit on iOS 7
if ([self respondsToSelector:@selector(setFooForExtendedLayout:)])
{
self.fooForExtendedLayout = UIFooEdgeLeft | UIFooEdgeRight;
}
float bottomOffset = 0;
// Only run this bit on iOS 7, else leave bottomOffset as 0
if ([self.hostController respondsToSelector:@selector(bottomLayoutFoo)])
bottomOffset = self.hostController.bottomLayoutFoo.length;
(obfuscated to avoid breaking NDA)
(混淆以避免破坏 NDA)
XCode Errors:
XCode 错误:
Property 'fooForExtendedLayout' not found on object of type 'UIViewController *'
Use of undeclared identifier 'UIFooEdgeLeft'
Use of undeclared identifier 'UIFooEdgeRight'
Property 'bottomLayoutFoo' not found on object of type 'UIViewController *'
在“UIViewController *”类型的对象上找不到属性“fooForExtendedLayout”
使用未声明的标识符 'UIFooEdgeLeft'
使用未声明的标识符“UIFooEdgeRight”
在“UIViewController *”类型的对象上找不到属性“bottomLayoutFoo”
It would be a pain to comment out this new code. What is the correct way to re-write it to be compatible with both the old and new Base SDKs, and does submitting it now (via XCode 4 and built against iOS 6 SDK) risk any sort of App Store rejection?
注释掉这个新代码会很痛苦。重新编写它以与旧的和新的 Base SDK 兼容的正确方法是什么,现在提交它(通过 XCode 4 并针对 iOS 6 SDK 构建)是否有任何 App Store 拒绝的风险?
采纳答案by Macmade
I would advise to wait until iOS 7 is ready to submit your update.
However, they are ways to fix the issue.
我建议等到 iOS 7 准备好提交您的更新。
但是,它们是解决问题的方法。
Property 'fooForExtendedLayout' not found on object of type 'UIViewController *'
在“UIViewController *”类型的对象上找不到属性“fooForExtendedLayout”
As properties are just a syntactic sugar, the easy way to fix this kind of error is to use a selector to call the method (setter):
由于属性只是一个语法糖,修复这种错误的简单方法是使用选择器来调用方法(setter):
[ self performSelector: NSSelectorFromString( "setFooForExtendedLayout:" ) withObject: ( id )xxx ];
@selector()
can't be used since you're asking for an iOS 7 selector with an iOS 6 SDK.
Hence the use of NSSelectorFromString
.
The withObject
argument is made for objects, as it names implies. But as objects are pointers, and as your method takes an enum value, you can actually pass it without problem, using a cast.
@selector()
无法使用,因为您要求使用 iOS 6 SDK 的 iOS 7 选择器。
因此使用NSSelectorFromString
. 顾名思义,
该withObject
参数是针对对象进行的。但是由于对象是指针,并且您的方法采用枚举值,因此您实际上可以使用强制转换毫无问题地传递它。
Use of undeclared identifier 'UIFooEdgeLeft'
Use of undeclared identifier 'UIFooEdgeRight'
使用未声明的标识符 'UIFooEdgeLeft'
使用未声明的标识符 'UIFooEdgeRight'
Now about your enum values, there's no such trick.
The only way is to declare them, with the same values as in the iOS 7 SDK, and pray that it won't change until the official release.
现在关于您的枚举值,没有这样的技巧。
唯一的方法是声明它们,使用与iOS 7 SDK中相同的值,并祈祷它在正式发布之前不会改变。
So now it's up to you... Personally, I would wait.
所以现在由你决定......就我个人而言,我会等待。
回答by 0xced
Here is the recommended wayto have code that compiles on both iOS 6 and 7 SDK:
以下是在iOS 6 和 7 SDK 上编译代码的推荐方法:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeRight;
#endif
回答by asdfghjkl
You could use:
你可以使用:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif
回答by barrast
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif
回答by CouchDeveloper
Edit: this answer (and also the questions) is not relevant anymore since now Xcode 5 is released! Please consider this when down-voting ;)
编辑:这个答案(以及问题)不再相关,因为现在 Xcode 5 发布了!请在投票时考虑这一点;)
The rationale for using a SCM tool to get two different code branches is due to subtle issues that can arise when switching Xcode Developer Preview versions and a Xcode Release versions with an identicalXcode project and same code and resource base. The project file and storyboard or xib may be changed under the hood, and possibly may get corrupt. It's notbecause there would be no way to find a solution in code! Nonetheless, I wouldn't recommend to work with differentXcode versions on the identicalXcode project. This may cause you headache!
使用 SCM 工具获取两个不同代码分支的基本原理是由于在切换 Xcode Developer Preview 版本和具有相同Xcode 项目和相同代码和资源库的 Xcode Release 版本时可能出现的微妙问题。项目文件和故事板或 xib 可能会在幕后更改,并且可能会损坏。这不是因为没有办法在代码中找到解决方案!尽管如此,我不建议在同一个Xcode 项目上使用不同的Xcode 版本。这可能会让你头疼!
The correct way is to use a source code management tool, like git. You should have an "iOS 6" branch, where you maintain release versions, and you should have one or more development branches where you use prerelease versions of Xcode and iOS.
正确的方法是使用源代码管理工具,例如 git。您应该有一个“iOS 6”分支,您可以在其中维护发布版本,并且您应该有一个或多个开发分支,您可以在其中使用 Xcode 和 iOS 的预发布版本。