【问题标题】:Calling Objective-C initializer with variadic arguments使用可变参数调用 Objective-C 初始化程序
【发布时间】:2015-11-10 19:29:15
【问题描述】:

我正在尝试将一个 Objective-C 类(即 TSAlertView)重用于 Swift 项目。问题是该类使用带有可变参数的初始化程序。我遵循stackoverflow question 建议的相同方法,如果我使用 iPad Air,我的代码可以在 iOS 模拟器中运行,但如果我使用 iPad Retina 则不行。该代码在真正的 iPad 3 上也会崩溃。

我能够创建一个显示相同问题的玩具示例。

TestClass.h

#import <Foundation/Foundation.h>

@interface TestClass : NSObject

@property NSArray *titles;

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args;

@end

TestClass.m

#import "TestClass.h"

@implementation TestClass

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args {

    NSMutableArray *titles = [NSMutableArray array];

    if ((self = [super init])) {
        [titles addObject:title];

        id arg;
        if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { // this causes an EXC_BAD_ACCESS on iPad Retina
            [titles addObject:(NSString*)arg];

            while ( nil != ( arg = va_arg( args, id ) ) ) 
            {
                if ( ![arg isKindOfClass: [NSString class] ] )
                    return nil;

                [titles addObject:(NSString*)arg];
            }
        }
    }
    self.titles = [NSArray arrayWithArray:titles];
    return self;
}

@end

TestClass+Swift.swift

import Foundation

extension TestClass {
    convenience init(title:String?, otherButtonTitles:CVarArgType...)
    {
        self.init(title: title, otherButtonTitlesVA:getVaList(otherButtonTitles))
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let testObject1 = TestClass(title: "First", otherButtonTitles: "Second", "Third")
        let testObject2 = TestClass(title: "First") // this causes the initializer to crash on iPad Retina

        NSLog("%@", testObject1.titles)
        NSLog("%@", testObject2.titles)
    }
}

尝试使用EXC_BAD_ACCESS 创建testObject2 时代码崩溃。我的代码有什么问题吗?为什么 iPad Air 显示的行为与 iPad Retina 不同?

编辑:好的,我认为问题在于 Objective-C 代码需要一个 nil 终止列表。如何从 Swift 传递 nil 终止 va_list

【问题讨论】:

    标签: ios objective-c swift initializer variadic


    【解决方案1】:

    正如您已经知道的那样,Objective-C 代码需要 nil 终止名单。没有那个,

    的行为
    if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { ... }
    

    如果实际传递的参数列表已用尽,则未定义。

    nil 在 Objective-C 中是一个 NULL 指针,你可以将它附加到你的便利初始化器中:

    extension TestClass {
        convenience init(title:String?, otherButtonTitles : CVarArgType...)
        {
            let nullPtr = UnsafePointer<Void>()
            let otherTitles : [CVarArgType] = otherButtonTitles + [nullPtr]
            self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
        }
    }
    

    这似乎在 32 位和 64 位平台上都能正常工作。


    Swift 3 更新:

    extension TestClass {
        convenience init(title:String?, otherButtonTitles : CVarArg...)
        {
            let otherTitles : [CVarArg] = otherButtonTitles + [Int(0)]
            self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
        }
    }
    

    正如https://bugs.swift.org/browse/SR-5046 中提到的,使用大小为零的Int 是在 变量参数列表。

    【讨论】:

    • 太棒了!感谢UnsafePointer&lt;Void&gt;()!我没想到!
    • 注意到你最近的编辑在这里,它促使我尝试更好的方法来获取空指针。这似乎有效:OpaquePointer(nil as UnsafePointer?)
    • 没关系,返回 Optional&lt;OpaquePointer&gt;.none... 太糟糕了,这太尴尬了:/
    • @jtbandes:是的,而且OpaquePointer? 不符合CVarArg。与OpaquePointer(bitPattern: 0) 相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    相关资源
    最近更新 更多