将目标c代码添加到swift文件[关闭]

Posted

技术标签:

【中文标题】将目标c代码添加到swift文件[关闭]【英文标题】:Add objective c code to swift file [closed] 【发布时间】:2016-05-02 19:29:08 【问题描述】:

我需要以某种方式将以下 Objective-c 代码添加到 AppDelegate.swift 文件到函数中。

    // Define some colors.
    UIColor *darkGray = [UIColor darkGrayColor];
    UIColor *lightGray = [UIColor lightGrayColor];

    // Navigation bar background.
    [[UINavigationBar appearance] setBarTintColor:darkGray];
    [[UINavigationBar appearance] setTintColor:lightGray];

    // Color of typed text in the search bar.
    NSDictionary *searchBarTextAttributes = @
        NSForegroundColorAttributeName: lightGray,
        NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]
    ;
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
    .defaultTextAttributes = searchBarTextAttributes;

    // Color of the placeholder text in the search bar prior to text entry.
    NSDictionary *placeholderAttributes = @
        NSForegroundColorAttributeName: lightGray,
        NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]
    ;

    // Color of the default search text.
    // NOTE: In a production scenario, "Search" would be a localized string.
    NSAttributedString *attributedPlaceholder =
        [[NSAttributedString alloc] initWithString:@"Search"
    attributes:placeholderAttributes];
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
    .attributedPlaceholder = attributedPlaceholder;

什么是最好的解决方案?我应该只创建 Objective-C 文件还​​是头文件?

【问题讨论】:

直接翻译成swift,直接写在AppDelegate.swift中。 【参考方案1】:

这是对 Swift 的粗略翻译...未经测试。

// Define some colors.
var darkGray: UIColor = UIColor.darkGrayColor()
var lightGray: UIColor = UIColor.lightGrayColor()
// Navigation bar background.
UINavigationBar.appearance().barTintColor = darkGray
UINavigationBar.appearance().tintColor = lightGray
    // Color of typed text in the search bar.
var searchBarTextAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
    // Color of the placeholder text in the search bar prior to text entry.
var placeholderAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]

   // Color of the default search text.
    // NOTE: In a production scenario, "Search" would be a localized string.
var attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Search", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder

【讨论】:

是的,这是正确的。我只是不太擅长从 Objective-C 转换到 Swift。 请注意,大多数时候您不需要定义变量的显式类型——让 Swift 推断它即可。此外,您应该将let 用于您不变异的变量。

以上是关于将目标c代码添加到swift文件[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

将多个声音(目标 c)播放到 Swift(xcode 7)的代码帮助

C#使用while循环将项添加到List [关闭]

如何将代码 AVFoundation 目标 c 转换为 Swift?

如何在目标c中使用swift文件

如何将目标 C 推送通知代码转换为 Swift

无法将目标 C 代码段翻译成 Swift