Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 我如何重写这个ObjC方法;右按钮“;迅速地_Swift - Fatal编程技术网

Swift 我如何重写这个ObjC方法;右按钮“;迅速地

Swift 我如何重写这个ObjC方法;右按钮“;迅速地,swift,Swift,我想把下面的object-c代码重写成swift - (NSArray *)rightButtons { NSMutableArray *rightUtilityButtons = [NSMutableArray new]; [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]

我想把下面的object-c代码重写成swift

- (NSArray *)rightButtons
{
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
                                            title:@"More"];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                            title:@"Delete"];

    return rightUtilityButtons;
}
sw_AddUtilityButton的颜色定义如下:

#import "NSMutableArray+SWUtilityButtons.h"

@implementation NSMutableArray (SWUtilityButtons)

- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = color;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setAdjustsFontSizeToFitWidth:YES];
    [self addObject:button];
}

尤其是头文件名带有“+”标记,如何初始化“NSMutableArray+SWUtilityButtons.h”的实例?

足够简单。在Swift中添加一个扩展名,如下所示

extension NSMutableArray
{

func sw_addUtilityButtonWithColor(color : UIColor, title : String)
    {
        var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        button.backgroundColor = color;
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.adjustsFontSizeToFitWidth;
        self.addObject(button)
    }
}
var rightUtilityButtons : NSMutableArray = NSMutableArray()
rightUtilityButtons.sw_addUtilityButtonWithColor(UIColor.redColor(), title: "More")
可以从项目中的任何位置访问扩展。您不需要像在Objective-C中导入类别那样进行导入,所以只需这样使用即可

extension NSMutableArray
{

func sw_addUtilityButtonWithColor(color : UIColor, title : String)
    {
        var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        button.backgroundColor = color;
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.adjustsFontSizeToFitWidth;
        self.addObject(button)
    }
}
var rightUtilityButtons : NSMutableArray = NSMutableArray()
rightUtilityButtons.sw_addUtilityButtonWithColor(UIColor.redColor(), title: "More")