Ios Swift - Objective C Code Migration to Swift

Migrating from Objective-C to Swift and deleting already converted Objective-C files

I just deleted the Objective C file, after it was disassociated from the Xcode project. Thus MikeIOSMike was correct. Thank you MikeIOSMike for your response

Swift CocoaPod Library in Objective-C Project Migration from Swift 3 to 4/5

For anyone else that runs across this. The online resources I found were helpful but didn't detail this specific case.

  1. First off. Leave the CocoaPod code as it is. This will allow you to update it without having to worry about modifying it each time.

  2. Next create a Swift file within your Objective C Project. xCode will ask if you want a header generated for it, say yes. This file will be called -Swift.h.

  3. In this Swift file subclass the Swift file from the CocoaPod you are interested in accessing.

For example :

import Foundation
import HGCircularSlider

class CircularSliderObjc: RangeCircularSlider {
}

  1. Next add in the properties you wish to access with getter and setters.
    @objc override open var startPointValue: CGFloat {

get {
return super.startPointValue;
}

set {
super.startPointValue = newValue;
}
}

  1. Then finally change the import in your Objective-C file to your project's generated header file that I mentioned above (#import "-Swift.h"
    ). If you have a property pointing to the class in the CocoaPod change it to your new Swift Class. For example :
@property (weak, nonatomic) IBOutlet CircularSliderObjc *rangeSlider;

In this example I have it setup as an Outlet for InterfaceBuilder.

After that, you're done. It seems like a lot of work but it's quite easy and simple. Not quite as fast as just importing the header but since Swift 4 and 5 you can no longer just access open vars in Swift as properties in objc. They need the @objc declaration to make that work. Added security to the language I'm guessing.

Hope this helps someone.

Migrating to swift - Problem with completion Handlers

Hey it seems that compiler wants to know all the information to make these methods available to swift.

So instead of

@class VOCRequestConfiguration;
@class VOCNetworkManagerResponse;
@class VOCMultipartRequestConfiguration;

use

#import "VOCRequestConfiguration.h"
#import "VOCNetworkManagerResponse.h"
#import "VOCMultipartRequestConfiguration.h"

Porting Int Realm List from Objective-c to Swift

I found that I just had to add requiredProperties method on the RLMObject subclass in Objective-c version, and it will work fine in Swift version. The reason is that the List type must be non-optional.

+ (NSArray<NSString *> *)requiredProperties {
return @[@"dates"];
}

How to correctly migrate old data saved with NSKeyedArchiver into new format while migrating from Objective-C to Swift

After looking around for a bit, the actual solution is:

  1. Make sure public required init?(coder: NSCoder) in NewItem class properly unarchives all fields you want to preserve from OldItem. For instance enums, or number type conversions may require special handling, even if you didn't change fields per se.

  2. Right before unarchiving, tell NSKeyedUnarchiver that NewItem should be used in place of OldItem, using NSKeyedUnarchiver.setClass method.

Example:

let archivedData = try Data(contentsOf: filePath)
NSKeyedUnarchiver.setClass(NewItem.self, forClassName: "OldItem")
let items = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archivedData)

Code migration from Swift 2.x to Swift 4 (for loop)

Here's your statement in Swift 4:

for i in 1..<dataByLine.count {

}


Related Topics



Leave a reply



Submit