My iOS 10 and Swift 3 Refactor List

I used an early beta of Xcode 8 to begin my conversion to Swift 3, which led to some issues so I had to do a lot of manual changes. Actually the conversion tool still works well a line at a time. Next Error followed by Return (CMD-‘, Return) is quite handy.

I thought it would be useful to track the changes and keep a list in one place.

layerClass() method is now a variable:

override public class func layerClass() -> AnyClass {
    return CAEmitterLayer.self
}

override public class var layerClass: AnyClass {
    return CAEmitterLayer.self
}

Method signature in AppDelegate changed to:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

Still unresolved: ??

CGAffineTransform.scale(transform, scale, scale)
CGAffineTransform.scaledBy(transform, scale, scale) ???
transform = CGAffineTransform(scaleX: scale, y:scale)

CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0)
context.setStrokeColor(red: 1, green: 0, blue: 1, alpha: 1)
NSMakeRange(col, 1) // Unresolved
substringWithRange()
let x = rowString.substring(with: <#T##Range#>)
UIFont *aFont = [UIFont fontWithName:"American Typewriter" size:fontSize]
let aFont = UIFont(name:"American Typewriter", size:fontSize)
NSDictionary *attributes = @{NSFontAttributeName:  aFont}
let attributes = [NSFontAttributeName:  aFont]
UIRectCornerAllCorners
UIRectCorner.allCorners
xmlText.componentsSeparatedByString("\n")
xmlText.components(separatedBy:"\n")
String(contentsOfURL: xmlUrl)
String(contentsOf: xmlUrl)

url.componentsSeparatedByString("/")
url.components(separatedBy: "/")
NSTextAlignment.Center
NSTextAlignment.center
forState: .Normal
forState: .normal
.TouchUpInside 
.touchUpInside
url.rangeOfString("github.com")
url.contains("github.com")
// https://www.hackingwithswift.com/swift3
stringByReplacingOccurrencesOfString(" ", withString:"_")
"Hello, world".replacingOccurrences(of: "Hello", with: "Goodbye")
// https://swift.org/migration-guide/
DispatchQueue.GlobalAttributes.qosDefault
        DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {

}

UIApplication.sharedApplication().
UIApplication.shared.

UIDevice.currentDevice().
UIDevice.current.
UIBezierPath(ovalInRect
UIBezierPath(ovalIn

touchesBegan() signature changed?

   override func touchesBegan(touches: Set?, withEvent event: UIEvent?) {

moveToPoint()
move(to:
addLineToPoint()        
addLine(to: 
closePath()
close()
CGGradientCreateWithColors
CGColorSpaceCreateDeviceRGB
drawInRect
.draw(in: 
CGRectGetMidX()
CGRectGetMidY()

self.midX
self.midY
CGContextStrokePath(context)
context.strokePath()
CGContextSetLineWidth(context, 20)
context.setLineWidth(20)
CGRectInset()
self.bounds.insetBy(dx: 3, dy: 3)
self.view.bringSubviewToFront(cardFront)
self.view.bringSubview(toFront: cardFront)

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:CellIdentifier)

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier:CellIdentifier)

self.showViewController(viewController, sender: self)
self.show(viewController, sender: self)
lowercaseString
lowercased()
//http://stackoverflow.com/questions/38964264/openurl-in-ios10

UIApplication.shared.openURL(URL(string: appStoreUrl)!)
UIApplication.shared.open(URL(string:appStoreUrl)!, options: [:])

type(of:)

Autolayout issue Xcode 8 beta 6: _SwiftValue nsli_superitem

I’m converting my Objective C and Swift 2.x code to Swift 3 and I started getting this strange error:

[_SwiftValue nsli_superitem]: unrecognized selector sent to instance 0x610000056740

This occurred in my NSLayoutConstraint.constraints() methods:

[constraintString1, constraintString2].forEach {

    let constraint = NSLayoutConstraint.constraints(withVisualFormat: $0, options: [], metrics: metrics, views: viewDictionary)

   NSLayoutConstraint.activate(constraint)

}

Currently, I used inferred types on my viewDictionary and metrics variables:

let viewDictionary = [
"answerView1": self.answerView1,
"answerView2": self.answerView2,
"answerView3": self.answerView3,
"answerView4": self.answerView4,
]

let metrics = [:]

The workaround seems to be to provide explicit types on these two variables:

let viewDictionary:[String:UIView] = [
"answerView1": self.answerView1,
"answerView2": self.answerView2,
"answerView3": self.answerView3,
"answerView4": self.answerView4,
]

let metrics:[String:AnyObject] = [:]

There’s probably a deeper issue here but at this point, I’m simply glad to have my app running again. I’ll post more if I figure out any additional details.

5000 Swift URLs

Another 3 months and another 1000 URLs added to the collection.

http://www.h4labs.com/dev/ios/swift.html

There haven’t been any updates to the “search engine” in the past 3 months, and I don’t have much to add at this point that hasn’t already been said in the previous announcements:

https://h4labs.wordpress.com/2016/04/19/4000-swift-urls/

https://h4labs.wordpress.com/2016/01/18/3000-swift-urls/

https://h4labs.wordpress.com/2015/09/10/1500-swift-urls/

Please let me know if anyone has any feature requests.  Also, don’t forget that all the URLs are in a tsv on Github:

https://github.com/melling/SwiftResources

 

 

Ghost in the Microwave

microwave2

This morning our 8 year old (2008) GE microwave started turning on by itself in “Canned Vegetable” mode, a mode I don’t think we’ve ever used. I unplugged it several times for several minutes at a time, hoping the “reboot” might clear it up but it didnt’work.  At this point, I simply don’t trust it.

Anyway, I’m glad I was home. I don’t know if these things can start fires but having the microwave running for hours while we are away can’t be good.  Here’s the microwave information, in case you are experiencing a similar problem:

Model No: JVM1750SM1SS

Made in Malaysia

From what I’ve been reading on the Internet, it seems like the main circuit board is the culprit.

Someone else with the same model had the exact same complaint:

http://www.saferproducts.gov/ViewIncident/1466299

However, it does seem to be a common problem in other GE models:

4000 Swift URLs

The Swift Resources page reached 4000 URLs yesterday.

http://www.h4labs.com/dev/ios/swift.html

It has been 3 months since 3000 URLs, so Swift content is being generated quite quickly. iOS 10 will be announced in less than 2 months. At the current rate, we should expect 1000 iOS 10 Swift blogs and Github projects before it’s released in September.

In addition, over the past few months, I’ve added paywall links, although I haven’t gone back to add all the links. The content created by https://www.raywenderlich.com and http://nsscreencast.com/episodes is exceptional and well worth the money.

Finally, the iOS Swift books now has a search feature and I’m in the process of adding discount codes.

http://www.h4labs.com/dev/ios/books

If you know of a book not on the list, please let me know.

We’re approaching the start of Swift’s third year, and it looks like everything is in place to accelerate its already rapid adoption.

The Convergence of Mobile Phones and Desktop Computers

I’ve been telling people for over 15 years that someday we’ll have smartphones that will transform into desktop computers. It’s finally actually starting to happen with feature’s like Microsoft’s Continuum.

My initial thought was that you’d simply place your phone next to an external keyboard, mouse, and monitor then they would all automatically and wirelessly connect to the phone.

This will happen but now there’s another far more interesting possibility. Phones, always with you, simply innovate beyond what you can do with a desktop.

Consider technologies like Google Glass, Microsoft’s HoloLens, or Magic Leap will be the display, providing the large and augmented view.

magic_leap

For enhanced user input, a combination of voice and gesture recognition will allow people to make more use of our phones while on the go. Minority Report is 14 years old. Intel and Google, along with manufacturers of VR headsets. are making rapid progress on recognizing hand gestures.

This Google Soli video demonstrates the emerging possibilities:

Over a billion smartphones are sold every year, generating hundreds of billions in profits. The revenue is being used to fund a smartphone arms race to build fresh and innovative mobile technologies (e.g. Apple Pencil, 3d Touch, TouchId).

Individually, each of these technologies might seem minor, but in aggregate they’ll make the smartphone of today appear antiquated within a few years. After all, companies need to give you a reason to update every few years.

All of these enhancements will give you fewer reasons to sit down at your desktop or reach for your laptop.  Personal computers will soon be the pickup trucks of computing.

Should I Use Objective C or Swift for Writing iOS Apps?

Simply look at these code snippets:

@import UIKit; // Other imports below
#import “ViewController1.h”
#import “ViewController2.h”
#import “MyDataModel.h”
#import “NoLongerUsed.h”

NSString *s = @”Swift is the future”;
UIViewController *vc = [[UIViewController alloc] init];
UILabel *label1 = [[UILabel alloc] init];
UIButton *button1 = [[UIButton alloc] init];
NSArray *names = @[@”John”, @”Paul”, @”George”, @”Ringo”];
NSDictionary *ages = @{@”John”: @(1940), @”Paul”: @(1942), @”George”: @(1943), @”Ringo”: @(1940)};

vs

import UIKit // No other imports needed

let s = “Swift is the future”
let vc = UIViewController()
let label1 = UILabel()
let button1 = UIButton()
let names = [“John”, “Paul”, “George”, “Ringo”]
let ages = [“John”: 1940, “Paul”: 1942, “George”: 1943, “Ringo”: 1940]

Swift is less visually noisy. Now, imagine 100,000 of code of each language. Which is more maintainable?

Less code and more maintainable code directly translates to a cost savings. The only valid argument developers have is that Swift is still an evolving language so the source will break, at least in the next version. I claim that you’re still better off writing in Swift and fixing any breaking changes than writing in Objective C. You will have done less work and your code will be safer and more maintainable.

By the way, if you want to learn Swift, there’s a lot of information available: books, blogs, and other reference material.