2

What is the replacement of ObjC class method +load and +initialize in Swift?

Or how to do the same thing in Swift?

debuggenius
  • 449
  • 5
  • 15
  • Class methods `+load` and `+initialize` are not that common, so you might have to provide example of these methods. Regarding initializing an instance, in Objective-C, you'd usually you'd have `-init` methods (which are simply `init` functions in Swift). I'd suggest you clarify precisely what your hypothetical `+load` and `+initialize` methods are if you need more information. – Rob Sep 18 '14 at 02:52
  • This isn't a dupe of http://stackoverflow.com/questions/24137212/initialize-class-method-for-classes-in-swift because this asks about +load in addition to +initialize. These methods are clearly defined in Apple's [NSObject Reference](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/clm/NSObject/load) so I think the question is sufficiently clearly stated. – Daniel Dickison Sep 23 '15 at 23:00

1 Answers1

-4

This documentation may be helpful, if you're looking to call initializers on the Objective-C classes in UIKit and other Apple-provided APIs.

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

From that page, this short example. Instead of doing this in Objective C:

UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];   

do this in Swift:

let myTableView: UITableView = UITableView(frame: CGRectZero, style: .Grouped)

If you're looking for how to initialize a Swift object that you've written, that's performed using a declared initializer like so:

class myClass: mySuperClass {
  var x: init;

  init(Int x, Int y) {
    self.x = x;
    super.init(y:y);
  }
}

Ray Wenderlich's tutorials are a great jumping-off point for understanding the Swift language:

http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start

I hope that helps.