2

What is the Swift equivalent of the following expression:

static CGRect MYScaleRect(CGRect rect, CGFloat scale)
{
    return CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale);
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Michael
  • 32,527
  • 49
  • 210
  • 370
  • See [Type Methods section](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html) on Swift documentation. – Lorenzo B Mar 12 '15 at 13:54
  • if I remember correctly in swift its called class not static (so class func foo not static func foo). See this http://stackoverflow.com/questions/24087936/how-do-i-make-class-methods-properties-in-swift – Abdul Ahmad Mar 12 '15 at 13:57

3 Answers3

5

Your code is plain C; there's no Objective-C involved. Also, strictly speaking, it is not an expression.

It is the definition of a function for which no symbol is emitted (that's what static does in this context). So the function is only visible in the current compilation unit (the .c or .m file where it's defined). The function is not tied to some class.

The semantic Swift equivalent would be a plain swift function with the private access modifier.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
2

For this type of function (utility) I would recommend using the struct extension, but there are three ways.

Free function: (equivalent of the function from the question)

private func MYScaleRect(rect: CGRect , scale: CGFloat ) -> CGRect {
    return CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale)
}

Struct extension:

private extension CGRect {
    static func MYScaleRect(rect: CGRect , scale: CGFloat ) -> CGRect {
        return CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale)
    }
}

Class method:

private class func MYScaleRect(rect: CGRect , scale: CGFloat ) -> CGRect {
    return CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale)
}

For this type of function (utility) I would recommend using the extension.

Tomasz Bąk
  • 6,124
  • 3
  • 34
  • 48
  • This answer does not describe the *equivalent* of the code in the question. It proposes two ways to **expose** the function instead of hiding it, as the original code does. – Nikolai Ruhe Mar 12 '15 at 14:13
  • You are right, I have added all possibilities to the answer to let the reader decide what he needs. – Tomasz Bąk Mar 12 '15 at 14:34
  • Regarding your proposals to use a more language conforming solution: An extension on the struct seems reasonable. I don't see why you are proposing a type method, though. You would rather use a normal instance method. The second proposal "Class method" does not make sense at all. In which class would you put this method? And why? – Nikolai Ruhe Mar 12 '15 at 16:50
  • Class method doesn't make sense to me in this context too, as it obfuscates the purpose of the method and hides possibility of the code reuse. It's in my answer only to present the language feature. – Tomasz Bąk Mar 13 '15 at 09:06
0

If your method belongs to a class, in Swiftyou can use type method:

class func MYScaleRect(rect: CGRect , scale: CGFloat )-> CGRect {}

Instance methods, as described above, are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods for classes by writing the keyword class before the method’s func keyword, and type methods for structures and enumerations by writing the keyword static before the method’s func keyword.

Jérôme
  • 8,016
  • 4
  • 29
  • 35