Swift Function Algorithm - swift

when I run this method in my project, the "name" variable is empty
Example :
import UIKit
class userDetail {
static var name : String?
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func OK(sender: UIButton){
getUserDetail()
print(userDetail.name)
// The first click is nil, the second click is print "test". How can I solve this problem?
}
func getUserDetail() {
userDetail.name = "test"
}
}

You have to instantiate it, as most of the comments suggest.
You need to do:
var userInfo = userDetail()
userInfo.name = "Test"
print(userInfo.name!)
As others suggested, you need to make class names (userDetail) capital. This is because class names start with class names. (UserDetail)

Related

access a textfield from a nsobject class

I am trying to access a textfield from a nsobject class. It's causing a runtime error of can't find txt in scope.
I tried having class coredata be inherited but that does not work. I don't really know what to do next. I may have to use parameter to pass the textfield but I am not sure how that would work.
import UIKit
class ViewController: UIViewController {
var txt = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class coreDataHandler : NSObject {
class func saver(){
txt.text = "d"
}
}

Add up integers in different View Controllers

I would like to be able to add up integers from buttons in View Controller 1 to a label in View Controller 2. I´m trying to pass the numbers through a separate swiftFile.
I thought I had the right code for this, but the numbers dont add together in the totalSumLabel.
describing is forced by X-cide. without it I get the error:
"init has been renamed to init(describing)"
inport UIKit
class addPrice: NSNumber {
var allSum: Int = 0
}
The code from View Controller 1:
class ViewController1: UIViewController {
var myTotal: addPrice?
#IBAction func button1(_ sender: UIButton) {
myTotal?.allSum += 190
}
#IBAction func button2(_ sender: UIButton) {
myTotal?.allSum += 240
}
The code from View Controller 2:
class ViewController2: UIViewController {
var myTotal: addPrice?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
totalSumLabel.text = String (describing: myTotal?.allSum)
I would implement a service like this:
import Foundation
import UIKit
class DataFactory {
var allSum:Int = 0
static let sharedInstance : DataFactory = {
let instance = DataFactory()
return instance
}()
}
And you can get your property value from every ViewController like this:
let allSum = DataFactory.sharedInstance.allSum
Or set your property like this from everywhere:
DataFactory.sharedInstance.allSum += 1
Its not a good idea to save global properties in specific ViewControllers

How to set up a NSComboBox using prepare for segue or init?

I am really struggling to pass the contents of one array from a view controller to another to set up the contents of a nscombobox. I have tried everything I can think of, prepare for segue, init; but nothing seems to work.
the program flow is as follows: the user enter a number into a text field and based on it an array with the size of the number is created. Once the user presses a button the next VC appears that has a combo box and inside that combo box those numbers need to appear. All my attempts result in an empty array being passed. Could someone please take a bit of time and help me out. Im sure I'm doing a silly mistake but cannot figure out what.
Code listing below:
Class that take the user input. At this stage I'm trying to pass the contents of the array in the next class as I gave up on prepare for segue because that one crashes because of nil error. Please note that prepare for segue is uncommented in the code listing just for formatting purposes here. Im my program it is commented out as I am using perform segue at the moment.
Any solution would be nice please. Thank you.
import Cocoa
class SetNumberOfFloorsVC: NSViewController {
//MARK: - Properties
#IBOutlet internal weak var declaredNumber: NSTextField!
internal var declaredFloorsArray = [String]()
private var floorValue: Int {
get {
return Int(declaredNumber.stringValue)!
}
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction private func setNumberOfFloors(_ sender: NSButton) {
if declaredNumber.stringValue.isEmpty {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please specify the number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else if floorValue == 0 || floorValue < 0 {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please input a correct number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else {
for i in 0...floorValue - 1 {
declaredFloorsArray.append(String(i))
}
print("\(declaredFloorsArray)")
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
performSegue(withIdentifier: "set number of rooms", sender: self)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if segue.identifier == "set number of rooms" {
if let addRoomsVC = segue.destinationController as? SetNumberOfRoomsForFloorVC {
addRoomsVC.floorBox.addItems(withObjectValues: declaredFloorsArray)
}
}
}
}
this is the class for the next VC with the combo box:
import Cocoa
class SetNumberOfRoomsForFloorVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
//MARK: - Properties
#IBOutlet internal weak var floorBox: NSComboBox!
#IBOutlet private weak var numberOfRoomsTxtField: NSTextField!
internal var boxData = [String]()
//MARK: - Init
convenience init(boxData: [String]) {
self.init()
self.boxData = boxData
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
floorBox.usesDataSource = true
floorBox.dataSource = self
floorBox.delegate = self
print("\(boxData)")
}
#IBAction private func setRoomsForFloor(_ sender: NSButton) {
}
//MARK: - Delegates
func numberOfItems(in comboBox: NSComboBox) -> Int {
return boxData.count
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
return boxData[index]
}
}
First you should remove the following code.
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
I assume you think that the viewController you created here is passed to prepareForSegue. However the storyboard instantiates a new viewController for you.
After that you need to set your declaredFloorsArray as the the boxData of the new viewController in prepareForSegue and you should be good to go.

variable cannot be modified in another class

am trying to modify a variable in another class, but it doesn't change.
class ViewController: UIViewController {
var t = 1
override func viewDidLoad() {
super.viewDidLoad()
t = 9
var pp = action().test()
println(pp) // got “2” here
}
the above should print "10" , but it shows "2".
another swift file:
class action {
var k = ViewController().t
func test()->Int{
k++
return k
}
}
Anything I made wrong?
Thnaks.
When you have a class with properties, and create an instance of that class, properties are bound to the class instance and not the class type. So if you create 2 instances of the same class their properties are independent, so if you change a property in one instance, that won't affect the same property in the other instance.
Note: by convention, in swift type names always start in uppercase, so I have renamed action to Action in my code.
Your code isn't working as expected because in the Action class you are creating a new instance of ViewController:
var k = ViewController().t
which has no relationship with the instance used to instantiate Action - so the new instance will have its t property set to 1.
The correct way to fix it is to pass the view controller instance to Action, and let it work on that instance.
class ViewController: UIViewController {
var t = 1
override func viewDidLoad() {
super.viewDidLoad()
t = 9
var action = Action(viewController: self)
var pp = action.test()
println(pp) // got “2” here
}
}
class Action {
var k: Int
init(viewController: ViewController) {
self.k = viewController.t
}
func test()->Int{
k++
return k
}
}
The above code should give an indication of what's wrong with your code, but it can be written in a better way. Action doesn't really need the ViewController instance, it just needs an integer passed in to its initializer, so a better way to achieve the same result is by modifying the code as follows:
class ViewController: UIViewController {
var t = 1
override func viewDidLoad() {
super.viewDidLoad()
t = 9
var action = Action(t: self.t)
var pp = action.test()
println(pp) // got “2” here
}
}
class Action {
var k: Int
init(t: Int) {
self.k = t
}
func test()->Int{
k++
return k
}
}

Class Variable not Being Set

This is a weird one. I have several classes so far in a test app and everything has been going swimmingly. However, I'm getting the error EpisodePlayerController.Type does not have a member named episodeData when I declare otherThing below.
import UIKit
class EpisodePlayerController: UIViewController {
var episodeData = "Hi"
var otherThing = episodeData
}
Tried restarting Xcode, restarting Mac, recreating the class, renaming the class, etc. At a loss. Might be a bug in my Xcode install, but I'd love to be wrong and not have wait for another release. ;)
This seems to solve the problem:
class EpisodePlayerController: UIViewController {
var episodeData = "Hi"
var otherThing = ""
init(){
otherThing = episodeData
}
}
Take a look at the property reference.
The problem is that as far as Swift is concerned, none of your properties have values until the class is fully initialized. Before that point, you can't interact with 'self' implicitly. (this includes its properties and functions.) I think your class is a good candidate for the #lazy attribute:
import UIKit
class EpisodePlayerController: UIViewController {
var episodeData: String = "Hi"
#lazy var otherData: String = self.episodeData
}
You could also include more complex stuff if you need to:
import UIKit
class ClassTwo {
var episodeData = "Hi"
#lazy var otherData: String = {
// Additional parameters
return self.episodeData
}()
}
This way, you can access self because the property 'otherData' won't be called until 'episodeData' is fully initialized.
Because this view was being called via a segue, I wound up making the variable optional and then calling a function from the segue to set the optional variable and then trigger any function dependent on that variable:
import UIKit
class YourController: UIViewController {
var yourData:NSString?
func setYourData(value:NSString) {
self.yourData = value
dependentFunction()
}
A similar effect can be achieved using the didSet and/or willSet methods:
import UIKit
class YourController: UIViewController {
var yourData:NSString? {
didSet {
// Dependent functionality
}
}
func setYourData(value:NSString) {
self.yourData = value
}
Also, for reference, the segue looks like this:
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "yourSegue" {
var destination:YourController = segue.destinationViewController as YourController
destination.setYourData(someData)
}
}
i think this code will help.
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "my test square")
test.area()
test.simpleDescription()”