Change values of variables in class swift
Tag : class , By : Dave M
Date : March 29 2020, 07:55 AM
this will help I found the solution. You need to declare all fields and image view as lazy variables and work with them in init: import Foundation
import Cocoa
import AppKit
class Game {
init(num : CGFloat, picName : String, gameName : String, description : String, windowHeight : CGFloat, sender : NSViewController) {
self.number = num
self.pictureName = picName
self.name = gameName
self.desc = description
self.height = windowHeight
imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
nameField = NSTextField(frame: NSRect(x: 110, y: 60, width: 300, height: 30))
descField = NSTextField(frame: NSRect(x: 110, y: 0, width: 300, height: 60))
nameField.font = NSFont(name: "System", size: 15.0)
nameField.stringValue = self.name
nameField.alignment = NSTextAlignment(rawValue: 2)!
nameField.selectable = false
nameField.editable = false
nameField.backgroundColor = sender.view.window?.backgroundColor
nameField.bordered = false
descField.selectable = false
descField.editable = false
descField.stringValue = desc
descField.font = NSFont(name: "System", size: 11.0)
descField.backgroundColor = NSColor.controlColor()
descField.bordered = true
descField.backgroundColor = sender.view.window?.backgroundColor
imageView.image = NSImage(named: self.pictureName)!
gameView = NSView(frame: NSRect(x: 0, y: /*height - (100 * num)*/ height - (120 * num), width: 600, height: 100))
button = NSButton(frame: NSRect(x: 0, y: 0, width: 600, height: 100))
button.tag = Int(number)
button.action = nil
var gesture = NSClickGestureRecognizer(target: sender, action: Selector("buttonPressed:"))
gesture.numberOfClicksRequired = 1
gesture.buttonMask = 0x1
button.addGestureRecognizer(gesture)
button.bezelStyle = NSBezelStyle(rawValue: 6)!
button.transparent = true
gameView.addSubview(nameField)
gameView.addSubview(imageView)
gameView.addSubview(descField)
gameView.addSubview(button)
}
var number : CGFloat
var pictureName : String
var name : String
var desc : String
var height : CGFloat
lazy var button : NSButton = NSButton()
lazy var imageView : NSImageView = NSImageView()
lazy var nameField : NSTextField = NSTextField()
lazy var descField : NSTextField = NSTextField()
lazy var gameView : NSView = NSView()
}
var newGame = Game(num: number, picName: "Bomb2.png", gameName: "Bomber", description: "Just a simple application", windowHeight: size!.height, sender: self)
self.view.addSubview(newGame.gameView)
|
cannot change class variables while runtime swift
Date : March 29 2020, 07:55 AM
help you fix your problem That's an what I want to do. I want to change a class variable while the project is running and use that in player. For example, while the app is running when user plays a button the player will stop and change the URL and start again. How can I do it? , This, is not good, because you create a player with an invalid URL: // NOT GOOD
class PlayerAv {
var audioLink: String = ""
var player: AVPlayer
init()
{
player = AVPlayer(URL: NSURL(string: self.audioLink))
}
}
let myPlayer = PlayerAv()
// GOOD
class PlayerAv {
var audioLink: String?
var player: AVPlayer
init(link: String) {
self.audioLink = link
self.player = AVPlayer(URL: NSURL(string: link))
}
}
var myPlayer = PlayerAv(link: "http://yourFirstURLhere")
myPlayer.player.play()
myPlayer.player.pause()
myPlayer.player.volume = 2
myPlayer = PlayerAv(link: "http://yourNewURLhere")
myPlayer.player.play()
println(myPlayer.audiolink!)
|
Issue with using variables inside a class function (Swift Playgrounds)
Date : March 29 2020, 07:55 AM
I hope this helps . It does work. You can change the value of a in pressed. I added code to your pressed method, as follows: import UIKit
import PlaygroundSupport
class Responder: NSObject {
func pressed(sender: UIButton) {
a = a+1
print(a)
}
}
var a: Int = 0
let responder = Responder()
let bt = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0))
bt.addTarget(responder, action: #selector(Responder.pressed), for: .touchUpInside)
PlaygroundPage.current.liveView = bt
|
iOS Swift - Call function for another class and see null variables
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further i have three classes, where i have a container, mainview and a left menu. , I think problem is in this code: var viewController: ViewController = ViewController()
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewControllerIdentifier")
|
Appending to self has no effect after exiting class method
Date : March 29 2020, 07:55 AM
This might help you You are rebinding the name self to a new instance of Buffer then proceeding to invoke the append method on that new instance. Remember that self is just like any other name that can be rebound and when it is rebound it doesn't retain any special properties pointing back to the original object. The following line can be removed and your code should work as expected. self = Buffer() # In case self had previous content.
|