我想在info.plist中获取受支持的界面方向的item0。
为了拥有这样的东西:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
return supportedOrientations[0];
}
但是我当然有一个不兼容的类型错误:
Incompatible pointer to integer conversion returning 'id' from a function with result type
如何解决这个问题?
答案 0 :(得分:1)
就像@Mats在他的评论中提到的那样,信息词典包含受支持方向的字符串值。您需要将字符串值转换为所需的UIInterfaceOrientationMask
类型。您可以使用字符串类别来执行此操作。
NSString + UIDeviceOrientation.h
static NSString *kUIInterfaceOrientationPortrait = @"UIInterfaceOrientationPortrait";
static NSString *kUIInterfaceOrientationLandscapeLeft = @"UIInterfaceOrientationLandscapeLeft";
static NSString *kUIInterfaceOrientationLandscapeRight = @"UIInterfaceOrientationLandscapeRight";
static NSString *kUIInterfaceOrientationPortraitUpsideDown = @"UIInterfaceOrientationPortraitUpsideDown";
@interface NSString (UIDeviceOrientation)
- (UIInterfaceOrientationMask)deviceOrientation;
@end
NSString + UIDeviceOrientation.m
@implementation NSString (UIDeviceOrientation)
- (UIInterfaceOrientationMask)deviceOrientation {
UIInterfaceOrientationMask mask = UIInterfaceOrientationMaskAll;
if ([self isEqualToString:kUIInterfaceOrientationPortrait]) {
mask = UIInterfaceOrientationMaskPortrait;
}
else if ([self isEqualToString:kUIInterfaceOrientationLandscapeLeft]) {
mask = UIInterfaceOrientationMaskLandscapeLeft;
}
else if ([self isEqualToString:kUIInterfaceOrientationLandscapeRight]) {
mask = UIInterfaceOrientationMaskLandscapeRight;
}
else if ([self isEqualToString:kUIInterfaceOrientationPortraitUpsideDown]) {
mask = UIInterfaceOrientationMaskPortraitUpsideDown;
}
return mask;
}
快速版本
extension String {
private var kUIInterfaceOrientationPortrait: String {
return "UIInterfaceOrientationPortrait"
}
private var kUIInterfaceOrientationLandscapeLeft: String {
return "UIInterfaceOrientationLandscapeLeft"
}
private var kUIInterfaceOrientationLandscapeRight: String {
return "UIInterfaceOrientationLandscapeRight"
}
private var kUIInterfaceOrientationPortraitUpsideDown: String {
return "UIInterfaceOrientationPortraitUpsideDown"
}
public var deviceOrientation: UIInterfaceOrientationMask {
switch self {
case kUIInterfaceOrientationPortrait:
return .portrait
case kUIInterfaceOrientationLandscapeLeft:
return .landscapeLeft
case kUIInterfaceOrientationLandscapeRight:
return .landscapeRight
case kUIInterfaceOrientationPortraitUpsideDown:
return .portraitUpsideDown
default:
return .all
}
}
}