布局中的奇怪异常

Bim*_*awa 13 xcode nsautolayout swift

尝试调用此方法:

avatarIconImageView.setContentHuggingPriority(UILayoutPriorityDefaultLow, forAxis: UILayoutConstraintAxis.Horizontal)
Run Code Online (Sandbox Code Playgroud)

并捕获此异常:

架构armv7的未定义符号:
"_ UILayoutPriorityDefaultLow",引用自:__ TFCvatar_iOS36TCAvatarWithCounterUniversalCellView22configureBodyCellViewsfS0_FT_T_ in TCAvatarUniversalCellView.o ld:未找到架构armv7 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

这是什么意思?

Nic*_*der 20

这看起来像是iOS 8 SDK中的一个错误.您可以通过传入原始值来解决此问题.

  • UILayoutPriorityDefaultRequired = 1000
  • UILayoutPriorityDefaultHigh = 750
  • UILayoutPriorityDefaultLow = 250

在你的情况下

avatarIconImageView.setContentHuggingPriority(250, forAxis: UILayoutConstraintAxis.Horizontal)
Run Code Online (Sandbox Code Playgroud)


Jas*_*oss 7

这不是一个错误.期望将Objective-C库导入Swift是一个缺点.应该理解Swift如何将代码(甚至从Apple UIKIt Libraries)导入Objective-C到Swift.

UILayoutPriority是一个浮点数.在Objective-C中,为我们预先定义了几个值.预定义的值似乎是枚举.我们可能期望在Swift中可以使用相同的枚举.

文档提出了一个枚举:

声明SWIFT

typealias UILayoutPriority = Float
Run Code Online (Sandbox Code Playgroud)

Objective-C的

enum {
   UILayoutPriorityRequired = 1000,
   UILayoutPriorityDefaultHigh = 750,
   UILayoutPriorityDefaultLow = 250,
   UILayoutPriorityFittingSizeLevel = 50,
};
typedef float UILayoutPriority;
Run Code Online (Sandbox Code Playgroud)

但是在Xcode中,如果你要求查看其中一个枚举值(例如UILayoutPriorityRequired)的定义,你会发现它们实际上是在头文件中定义为常量浮点数.

typedef float UILayoutPriority;
static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = 1000; // A required constraint.  Do not exceed this.
static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = 750; // This is the priority level with which a button resists compressing its content.
static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = 250; // This is the priority level at which a button hugs its contents horizontally.
Run Code Online (Sandbox Code Playgroud)

因此,尽管我们可能会将预定义的布局优先级视为枚举值(如文档所示),但布局优先级并未真正定义为枚举; 它们被定义为常量浮点数.

任何知道C编程语言的人的提示都是C enum只能包含int值.以下是合法的,将编译:

enum myEnum {
    JGCEnum_one = 1,
    JGCEnum_two,
    JGCEnum_three
} JGCEnum;
Run Code Online (Sandbox Code Playgroud)

但我们无法将浮点数定义为C枚举的值.以下内容无法编译:

    enum myEnum {
        JGCEnum_one = 1.5, // compilation error
        JGCEnum_two,
        JGCEnum_three
    } JGCEnum;
Run Code Online (Sandbox Code Playgroud)

Objective-C枚举与C枚举相同(Swift枚举不同).重要的是要知道我们是否正在处理实际的整数或浮点数.因为可以使用NS_ENUM宏定义整数,然后可以将其作为Swift枚举导入Swift.

iBook说

Swift作为Swift枚举导入任何标有NS_ENUM宏的C样式枚举.这意味着枚举值名称的前缀在导入Swift时会被截断,无论它们是在系统框架中还是在自定义代码中定义的.

摘录自:Apple Inc."将Swift与Cocoa和Objective-C结合使用."iBooks.https://itun.es/us/1u3-0.l

这意味着,如果UILayoutPriority 被定义为使用NS_ENUM宏观整数,它会被导入到斯威夫特斯威夫特枚举.这是UILayoutConstraintAxis的情况.

声明SWIFT

enum UILayoutConstraintAxis : Int {
    case Horizontal
    case Vertical
}
Run Code Online (Sandbox Code Playgroud)

Objective-C的

enum {
   UILayoutConstraintAxisHorizontal = 0,
   UILayoutConstraintAxisVertical = 1
};
typedef NSInteger UILayoutConstraintAxis;
Run Code Online (Sandbox Code Playgroud)

查看Objective-C头文件确认文档说的内容.

//
// UIView Constraint-based Layout Support
//

typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
    UILayoutConstraintAxisHorizontal = 0,
    UILayoutConstraintAxisVertical = 1
};
Run Code Online (Sandbox Code Playgroud)

因此,至少有两种方法可以知道在Swift中是否可以使用在Objective-C中使用的预定义值:

  1. 检查文档
  2. 检查Objective-C中的头文件(通过右键单击该值,然后选择"跳转到定义"找到)

还有一种方法可以查看您习惯使用的typedef是常量还是枚举.在代码中,测试以查看常量的地址是否存在.常量有一个内存地址,而枚举没有.请参阅下面的代码.

// this line will compile and run just fine. 
// UILayoutPriorityDefaultHigh is a constant and has a memory address
// the value will be true if the device is running iOS 6.0 or later
// and false otherwise
BOOL predefinedValueIsAvailable = (NULL != &UILayoutPriorityDefaultHigh);

// this line will not compile
// UILayoutConstraintAxisHorizontal is an enum (NOT a constant) 
// and does not have a memory address
predefinedValueIsAvailable = (NULL != &UILayoutConstraintAxisHorizontal);
Run Code Online (Sandbox Code Playgroud)

参考