Swift 3 subclassing OperationQueue - nsoperationqueue

I have some problem with migration my code to Swift 3 (Xcode 8 beta 4).
import Foundation
class MyOperationQueue: Foundation.OperationQueue {
override func addOperation(_ op: Operation) {
....
}
I'm receiving this two errors:

Found solution:
override func addOperation(_ op: Foundation.Operation) {
....
}

Related

Swift 3 - How to use enum raw value as NSNotification.Name?

I'm using Xcode 8 beta 5 and I'm trying to setup an enum of notifications like this
enum Notes: String {
case note1
case note2
}
Then trying to use them as the notification names
NotificationCenter.default.post(name: Notes.note1.rawValue as NSNotification.Name,
object: nil, userInfo: userInfo)
But I'm getting an error.
Cannot convert value of type 'String' to specified type 'NSNotification.Name'
Is there a work around, or am I missing something? It works in Xcode 7.3.1
Any help would be appreciated.
Here you go, Use Swift 3 & Xcode 8.0
enum Notes: String {
case note1 = "note1"
case note2 = "note2"
var notification : Notification.Name {
return Notification.Name(rawValue: self.rawValue )
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.post(name: Notes.note2.notification ,object: nil, userInfo: nil)
}
}
Another way
import UIKit
extension Notification.Name
{
enum MyNames
{
static let Hello = Notification.Name(rawValue: "HelloThere")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.post(name: Notification.Name.MyNames.Hello ,object: nil, userInfo: nil)
}
}
I am doing this way, For me this is more simple way to manage Notification names.
Swift 3.0 and Xcode 8.0
Using extension of Notification.Name, we can define static names inside that as following.
extension Notification.Name {
static let newPasscodeSet = Notification.Name("newPasscodeSet")
static let userLoggedIn = Notification.Name("userLoggedIn")
static let notification3 = Notification.Name("notification3")
}
We can use that names like this:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(self.newPasscodeSetAction), name: .newPasscodeSet, object: nil)
}
func newPasscodeSetAction() {
// Code Here.
}
Hope this simple way helpful for you.
As far as I know, there was no type NSNotification.Name in Swift 2.2.1/SDKs bundled in Xcode 7.3.1, so I'm curious how you have made it work.
Anyway you need to write something like this if you want to utilize your enum:
NotificationCenter.default.post(name: NSNotification.Name(Notes.note1.rawValue),
object: nil, userInfo: userInfo)
By the way, my best recommendation to define your own Notification.Name is using extension which defines static properties:
extension Notification.Name {
static let note1 = NSNotification.Name("note1")
static let note2 = NSNotification.Name("note2")
}
(It's a little bit longer than enum..., but) you can use it like this:
NotificationCenter.default.post(name: .note1,
object: nil, userInfo: userInfo)
Maybe another approach in swift 4.2
extension Notification.Name{
struct RecordListNotification {
static let recordListDidChange:Notification.Name = Notification.Name("recordListDidChange")
static let recordListTimeDidChange = Notification.Name("recordListTimeDidChange")
}
}
and then
NotificationCenter.default.post(name: Notification.Name.RecordListNotification.recordListTimeDidChange, object: nil)
also to avoid verbose:
typealias RecordListNotification = Notification.Name.RecordListNotification
And it can be used:
NotificationCenter.default.post(name: RecordListNotification.recordListTimeDidChange, object: nil)

HKWorkoutSessionDelagate protocol conformity

I'm upgrading a watchOS2 to watchOS3 with xCode 8.0 Beta And I'm having trouble with HKWorkoutSessionDelagate. See image.
The fix-it suggestion, crashes xcode - anyone having similar issues or can anyone point me in the direction of the resolution - that would be much appreciated.
Thanks
Just added this delegate, I didn't get any error. Using Xcode 8.0, Swift 3.0.
//
// InterfaceController.swift
// asf WatchKit Extension
//
// Created by Alvin Varghese on 27/06/16.
// Copyright © 2016 Swift Coder. All rights reserved.
//
import WatchKit
import Foundation
import HealthKit
extension InterfaceController : HKWorkoutSessionDelegate
{
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date)
{
}
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: NSError){
}
}
class InterfaceController: WKInterfaceController {
override func awake(withContext context: AnyObject?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}

Non-generic Swift class can implement UIPickerViewDataSource but Generic version can't

Swift 1.2 / Xcode 6.3.
Why is this valid:
class RangeDelegateNongeneric: NSObject, UIPickerViewDataSource {
var values = [Int]()
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return values.count
}
}
but this isn't:
class RangeDelegateGeneric<T>: NSObject, UIPickerViewDataSource {
var values = [T]()
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return values.count
}
}
Error: Type RangeDelegateGeneric<T> does not conform to protocol UIPickerViewDataSource
Even more oddly, the Fix-it message: Candidate is not #objc, but protocol requires it prepends #objc to the beginning of each function, but that doesn't Fix-it, and the Fix-it tool is happy to repeatedly prepend #objc!
This is fixed in Swift 2.0, specifically tested in Xcode 7 (beta 5 as of this writing).
99% sure this blog fragment explains it, although a more compiler-nerd-friendly explicit spec statement is bizarrely difficult to find:
https://developer.apple.com/swift/blog/?id=29
[Emphasis mine]
Swift-er SDKs: Swift 2 works even better with the Apple SDKs, thanks
in part to two new features in Objective-C: nullability annotations
and generics. The SDKs have been updated to annotate API that cannot
return nil so you don’t need to use optionals as often. And with a
true generics system employed by the SDKs you can more often preserve
detailed type information in your Swift 2 code.

How to schedule a piece of code to execute in the next runloop in Swift?

There is no [NSRunLoop -performSelector:target:argument:order:modes:] in Swift currently. So does the [NSObject -performSelector:withObject:afterDelay:]. How can I schedule a piece of code to execute in the next runloop in Swift?
In Swift 3 and 4:
DispatchQueue.main.async() {
// ...
}
In Swift version below 3:
dispatch_async(dispatch_get_main_queue()) {
// ...
}
Swift 3
DispatchQueue.main.async() {
// ...
}
I figured out how to do so. Just enqueue a block based operation on the main queue with OperationQueue(NSOperationQueue in Cocoa).
OperationQueue.main.addOperation { () -> Void in
// Do your things
}

Use of unresolved identifier 'dispatch_get_main_queue' using SWIFT

I am having difficulty using 'dispatch_get_main_queue()' in SWIFT with the base SDK set to 10.9. The code that I am using is a simple dispatch_async call shown below:
dispatch_async(dispatch_get_main_queue()){
// Code here
}
However it is generating the error: Use of unresolved identifier 'dispatch_get_main_queue'. It works as expected when the base SDK is set to 10.10, but not at 10.9. I use it in objective-c in non-swift applications which use the base SDK 10.9 but I cannot figure out why it is not working with Swift.
I have tried to look into it but have currently been unable to find anything about why this is happening and what I can do to resolve it/achieve similar functionality. I would be very grateful if anyone can shed any light on this or point me in the right direction.
So my question is: Is there a way I can resolve this (or at least achieve similar functionality)?
EDIT 1 -
This is the full function I have created:
func addSubview(aView: NSView, fillView: Bool, removeExisting: Bool)
{
dispatch_async(dispatch_get_main_queue()){
if (aView != nil)
{
if (removeExisting)
{
for (var subview: NSView) in self.subviews() as NSView[]
{
subview.removeFromSuperview();
}
}
if (fillView)
{
aView.setAutoresizingMask(NSViewWidthSizable | NSViewHeightSizable);
aView.setFrame(NSMakeRect(0, 0, self.frame().size.width, self.frame().size.height));
}
self.addSubview(aView);
}
}
}
EDIT 2 - Replaced 'if (aView != null)' to 'if (aView != nil)' as suggested. I am not sure how I managed to make that rookie error!
Use it in a function. This should work:
func test() {
dispatch_async(dispatch_get_main_queue()) {
}
}
Works fine on my laptop with deployment target 10.9
Or in your app delegate maybe something like this:
func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
dispatch_async(dispatch_get_main_queue()) {
}
}
First you should replace if (aView != null) to if (aView != nil)
Swift uses nil not null
If view can be nil you should use optional type:
func addSubview(view: NSView?, fillView: Bool, removeExisting: Bool) {
dispatch_async(dispatch_get_main_queue()){
if let aView = view {
if removeExisting {
...

Resources