0

I am stuck on translating a method from the following bit.

In original Objective-C, the method is:

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

    return pointSize;
}

In Swift, I have done the following so far:

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

In particular, I don't know how to translate SLKPointSizeDifferenceForCategory, which is an unused 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;
}

Any guidance is much appreciated.

daspianist
  • 5,336
  • 8
  • 50
  • 94

2 Answers2

4

That's just a function that returns a 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 is just a flag that tells the compiler not to warn if that function is never used. See more here.

  • static just limits the scope of that function's existence to the current file.

  • UIContentSizeCategory is imported into Swift as a struct, which is more strongly typed than just using raw strings.

Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151
1

__unused only tells the compiler "don't warn me if I don't use this variable"., and in this context I believe that means no warning should be shown if the result of SLKPointSizeDifferenceForCategory goes unused.

As for the Slack SDK function you're trying to port, why not simply include SlackTextViewController in your bridging header and then you can pick up the original function like magic.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Nope, the [corresponding page for function attributes](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes) says: "the function is meant to be possibly unused". There is by default no warning for unused function results; the `warn_unused_result` attribute enables it. – jtbandes Apr 09 '17 at 02:47
  • There are certainly warnings for unused function results [(by default) in Swift](https://useyourloaf.com/blog/swift-3-warning-of-unused-result/) @jtbandes – Michael Dautermann Apr 09 '17 at 02:48