Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seasidekameel

macrumors newbie
Original poster
Jan 13, 2018
3
0
UK
Hello to all.....I am not totally new to programming but I am not that experienced so please be gentle !
I am developing a program, part of which has two sliders in a UI and a result text box. When the sliders are moved the result box is update as a result of a mathematical operation on the slider values (basically result = slider1 value * slider2 value) although it's a bit more involved than just multiplication...

I've been learning swift 4 / xcode 9 mostly for iOS since there are more tutorials but I want the program thi for desktop. This is my iOS code which works fine. When I use it in OS X (making necessary changes of UISlider to NSSlider, text to textString etc) it gives all host of errors :\
thank you in advance

Code:
import UIKit

class ViewController: UIViewController {
    
    
    let carbStep: Float = 5
    let flowStep: Float = 25
    var volumeContraction: Float = 0
    
    
    //carbonyl controls
    @IBOutlet weak var carbSlider: UISlider!
    
    @IBOutlet weak var carbValue: UILabel!
   
    @IBOutlet weak var flowSlider: UISlider!
  
    @IBOutlet weak var flowValue: UILabel!
  
    @IBOutlet var nickelPerDay: UITextField!
    
    func initialiseResult(){
        
        volumeContraction = (2500/(2500+(3*carbSlider.value))) // works in iOS error in OS X
        
        nickelPerDay.text = "\(Int(flowSlider.value*carbSlider.value*volumeContraction*(24/1000000)))"
        
    }
    
    @IBAction func sliderValueChanged(_ sender: UISlider) {
        let roundedCarbValue = round(sender.value / carbStep) * carbStep
        
        sender.value = roundedCarbValue
        
        carbValue.text = "\(Int(sender.value))"
   
        nickelPerDay.text = "\(Int(flowSlider.value*carbSlider.value*volumeContraction*(24/1000000)))"
        
    }
    @IBAction func flowValue(_ sender: UISlider) {
      
        let roundedFlowValue = round(sender.value / flowStep)*flowStep
        
        sender.value = roundedFlowValue
        
        flowValue.text = "\(Int(sender.value))"
        
        nickelPerDay.text = "\(Int(flowSlider.value*carbSlider.value*volumeContraction*(24/1000000)))"
        
    }
  
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //define some variables
        
        self.initialiseResult ()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
 
Your posted code won't work on an OSX Xcode project, because OSX app's don't have the UI controls that iOS has.
For example the UISlider would be an NSSlider, and the UILabel would be an NSTextField.
Also double check the GUI interface class documentation, as the iOS UI elements do have different class and instance functions to their NS class equivalents.

From memory I think the NSSlider does not have a ".value" function, but has ".intValue" ".integerValue" and a few others.

Your main ViewController class would also have to be changed from UIViewController, to an NSViewController subclass.

Your didReceiveMemoryWarning function would not be necessary at all, as NSApp's don't require such a function.

In short, code written for iOS would not always be relevant to OSX app's, as they have different classes and class functions.

Hope this helps.

Regards Mark
 
Last edited:
Your posted code won't work on an OSX Xcode project, because OSX app's don't have the UI controls that iOS has.
For example the UISlider would be an NSSlider, and the UILabel would be an NSTextField.
Also double check the GUI interface class documentation, as the iOS UI elements do have different class and instance functions to their NS class equivalents.

From memory I think the NSSlider does not have a ".value" function, but has ".intValue" ".integerValue" and a few others.

Your main ViewController class would also have to be changed from UIViewController, to an NSViewController subclass.

Your didReceiveMemoryWarning function would not be necessary at all, as NSApp's don't require such a function.

In short, code written for iOS would not always be relevant to OSX app's, as they have different classes and class functions.

Hope this helps.

Regards Mark


Hi, Mark,

Thanks for your help. I had changed the UI elements to NS elements but your second point is where I tripped, you are right, the .value function becomes double.Value in OSX etc. I've got it working now and learned a lot in the process ! Oddly enough when I corrected some stuff some of the other errors in some script lines disappeared even though some lines didn't change.
Incidentally do you know of any good learning resources for Swift 4 with OSX ? I have looked a lot on line but the vast majority of stuff is iOS and all the OS X stuff I found is a bit out of date, Swift 3 and earlier. I know mobile apps are the big thing but I'm trying to develop some basic programs for work and so they need to be desktop based.
 
Glad to hear you solved the problems with your code.

Yeah I found the same as you, a lot of the online tutorials are for iOS, I think it's because it's in more demand as a development platform.

I keep an eye on Ray Wenderlich's web site, although most of their tutorials are iOS, they do have some OSX content.
I found that some iOS code tutorials, can be adapted to OSX apps, but you do come across problems similar to your original posting, but Apple's developer documentation usually helps out with that.

Check out Ray Wenderlich's Tutorials link at the top of the page.

https://www.raywenderlich.com

Other than that I've tended to use the Big Nerd Ranch books, which are very good.

https://www.bignerdranch.com/books/

I tend to look for specific solutions for a particular coding problem, and Stack Overflow is always a good place for that.

https://stackoverflow.com

The Swift language itself does not change between iOS and OSX, it tends to be the framework classes that change, but are also similar in lots of ways.

Regards Mark
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.