使用Swift等效的未使用的静态更新Objective-C语法

时间:2017-04-09 01:52:17

标签: ios objective-c swift

我坚持从以下位翻译一个方法。

在最初的Objective-C中,方法是:

+ (CGFloat)defaultFontSize
{
    CGFloat pointSize = 16.0;
    NSString *contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
    pointSize += SLKPointSizeDifferenceForCategory(contentSizeCategory); //stucj here

    return pointSize;
}

在Swift中,到目前为止我已经做了以下事情:

let defaultFontSize: Float {
        get {
            let pointSize = 16.0
            let contentSizeCategory = UIApplication.shared.preferredContentSizeCategory
            //What do here for SLKPointSizeDifferenceForCategory?
     }
}

特别是,我不知道如何翻译SLKPointSizeDifferenceForCategoryunused static CGFloat

__unused static CGFloat SLKPointSizeDifferenceForCategory(NSString *category)
{
    if ([category isEqualToString:UIContentSizeCategoryExtraSmall])                         return -3.0;
    if ([category isEqualToString:UIContentSizeCategorySmall])                              return -2.0;
    if ([category isEqualToString:UIContentSizeCategoryMedium])                             return -1.0;
    if ([category isEqualToString:UIContentSizeCategoryLarge])                              return 0.0;
    if ([category isEqualToString:UIContentSizeCategoryExtraLarge])                         return 2.0;
    if ([category isEqualToString:UIContentSizeCategoryExtraExtraLarge])                    return 4.0;
    if ([category isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge])               return 6.0;
    if ([category isEqualToString:UIContentSizeCategoryAccessibilityMedium])                return 8.0;
    if ([category isEqualToString:UIContentSizeCategoryAccessibilityLarge])                 return 10.0;
    if ([category isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge])            return 11.0;
    if ([category isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge])       return 12.0;
    if ([category isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge])  return 13.0;
    return 0;
}

非常感谢任何指导。

2 个答案:

答案 0 :(得分:4)

这只是一个返回CGFloat的函数:

func SLKPointSizeDifference(for category: UIContentSizeCategory) -> CGFloat {
    typealias c = UIContentSizeCategory // for the sake of reducing boilerplate below

    switch category {
    case c.extraSmall:                         return -3
    case c.small:                              return -2
    case c.medium:                             return -1
    case c.large:                              return 0
    case c.extraLarge:                         return 2
    case c.extraExtraLarge:                    return 4
    case c.extraExtraExtraLarge:               return 6
    case c.accessibilityMedium:                return 8
    case c.accessibilityLarge:                 return 10
    case c.accessibilityExtraLarge:            return 11
    case c.accessibilityExtraExtraLarge:       return 12
    case c.accessibilityExtraExtraExtraLarge:  return 13
    default:                                   return 0
    }
}
  • __unused只是一个标志,告诉编译器不要警告该函数是否从未使用过。查看更多here

  • static只是将该函数的存在范围限制为当前文件。

  • UIContentSizeCategory作为结构导入到Swift中,它比仅使用原始字符串更强类型。

答案 1 :(得分:0)

__unused only tells the compiler "don't warn me if I don't use this variable".,在此背景下,我认为如果SLKPointSizeDifferenceForCategory的结果未使用,则表示不会显示任何警告。

As for the Slack SDK function you're trying to port,为什么不简单地在你的桥接头中包含SlackTextViewController,然后就可以像魔术那样选择原始函数。