logo
down
shadow

Using a function() variable in a different Class - Swift


Using a function() variable in a different Class - Swift

By : user2952045
Date : November 19 2020, 12:41 AM
I hope this helps . How can I use the value of getLevel() inside the customView class? This function is linked with an NSTimer. The values are the decibel values of an mp3 file, which I want to use inside customView to sync the decibels values with an animation , Comments in the code should be all you need to understand this:
code :
class CustomView: UIView {
    var percentage: CGFloat = 0.0 {
        didSet {
            setNeedsDisplay() // this will cause the system to tell the view to draw itself.
        }
    }

    func drawRect(rect: Rect) {
        // draw yourself based on percentage
    }
}

class ViewController: ... {
    func getLevel() {

        if (player.playing == true) {

            player.updateMeters()
            var dblevel = player.averagePowerForChannel(1)
            var percentage = CGFloat( pow(10, (0.05 * dblevel)) * 100)

            myCustomView.percentage = percentage // this is how to pass the information.
        }
    }
}


Share : facebook icon twitter icon
cannot pass a variable from a function in a class to another function in Swift

cannot pass a variable from a function in a class to another function in Swift


By : GhostSarge
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You should add the count variable at the very top of the file so that it is accessible within the file by all functions. This way you will only have one instance of count and you can update it from anywhere within the entire file. i.e. At the top of the file :
code :
var count : Int = calculateTime().count
update()
public class A {
    var count : Int = 0
}
public class B {
    var classA : A = A()

    print("Count : \(classA.count)")
}
public class C {
    var a : A = A()
    var b : B = B(class : a)
}

public class B {
    var count : Int

    init(class : A = A()) {
        count = class.count
    }
}
public class A {
    private var _count = 0

    var count : Int {
        get {
            return _count
        }

        set {
            if newValue != _count {
                _count = newValue
            }
        }
    }
}

public class B {
    var classA : A = A()

    classA.count = 2 // Would set the count of class A, for the particular instance you have created as 2
}
Is Class variable in swift can only be accessed by class function...?

Is Class variable in swift can only be accessed by class function...?


By : UsmanIdrees
Date : March 29 2020, 07:55 AM
Any of those help A class var belong to a class, not the instance. You can access it via:
the class name: MyClass.price self.dynamicType.price (for a function inside the class) myVar.dynamicType.price (assuming myVar is an instance of the class).
One variable, one class and two of the same function swift

One variable, one class and two of the same function swift


By : BeccaW
Date : March 29 2020, 07:55 AM
This might help you The problem here is that you are working with a single instance of Person. When you are doing:
code :
p.create("Isak", 11, "Boy")
p.create("Sanne", 7, "Girl") // Here I create another person
p.delete("Isak")
var p1 = Persons()
var p2 = Persons()
p1.create("Isak", 11, "Boy")
p2.create("Sanne", 7, "Girl") // Here I create another instance of person
p1.delete("Isak")
class Person {
var oldName = ""
var oldAge = 0
var oldGender = ""
var name = ""
var age = 0
var gender = ""

func create(_ name:String, _ age:Int, _ gender:String) -> String {

    if(age > 100) {
        print("Can't create a person with an age that is over 100!")

        return "Not created!"
    }

    if(gender == "Boy" || gender == "Girl") {

    }
    else {
        print("Gender has to be a girl or a boy!")

        return "Not created!"
    }
    self.name = name
    self.age = age
    self.gender = gender

    self.oldName = name
    self.oldAge = age
    self.oldGender = gender

    print("You just created a \(gender) called: '\(name)' with the age of: '\(age)'!")

    return "Created!"
}

func modify(_ targetName:String, _ newName:String, _ newAge:Int, _ newGender:String) -> String {

    if(age > 100) {
        print("Can't modify a person with an age that is over 100!")
        return "Not modified!"
    }

    if(gender == "Boy" || gender == "Girl") {

    }
    else {
        print("Gender has to be a girl or a boy!")
        return "Not modified!"
    }

    if(targetName != "" && targetName == self.name) {
        self.name = newName
        self.age = newAge
        self.gender = newGender

        print("You just changed \(oldName)'s gender from: \(oldGender) to: \(gender), \(oldName)'s name from: \(oldName) to: \(name) and \(oldName)'s age from: \(oldAge) to: \(age)!")
    }
    else {
        print("Please enter a valid target name or create one!")
    }

    return "Modified!"
}

func delete() -> String {
    return "Deleted!"
}
}

class Persons {
var personsDictionary = [String: Person]()
}

var persons = Persons()
var p1 = Person()
var p2 = Person()

p1.create("Isak", 11, "Boy")
p2.create("Sanne", 7, "Girl")

persons.personsDictionary[p1.name] = p1
persons.personsDictionary[p2.name] = p2

persons.personsDictionary["Isak"]?.delete()
How to wait for variable in Swift before executing function? (Swift)

How to wait for variable in Swift before executing function? (Swift)


By : user1375277
Date : March 29 2020, 07:55 AM
may help you . The function locationManager(_:didUpdateLocations:) is called every time when the location is update. So you should add downloadItems() in the location manager like this :
code :
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location: CLLocationCoordinate2D = manager.location?.coordinate else { return }

    // set the value of lat and long

    latitude = location.latitude
    longitude = location.longitude

    downloadItems()
}
Can I initialize a class with a variable which is defined by a function inside that class in Swift 2?

Can I initialize a class with a variable which is defined by a function inside that class in Swift 2?


By : Peter Gruenbeck
Date : March 29 2020, 07:55 AM
will help you There are two options.
First, if computeRandomString does not rely on knowing about anything about specific instances of this class, it could be converted to a class function, or simply moved outside of the class entirely.
Related Posts Related Posts :
  • Swift/iOS: How to access Object properties when type-casted from Protocol?
  • consecutive statement need to have ; error in swift
  • Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors
  • Does locationManager:didUpdateLocations: method called at the same time in extension and its containing app
  • Can't get Data using SwiftyJSON
  • Convert String to UnsafeMutablePointer<char_t> in Swift
  • inout param with empty object since nil won't work
  • How to align a mask onto an image
  • compiler forcing self inside static method of a function while using dispatch_sync
  • private static variable in struct
  • Conditional "in" for arrays in Swift
  • Extension for Double to accept String as initializer in Swift
  • Modifying UIButton Programmatically
  • Pass type to generic function and compare
  • How to customise the look of the PFSignUpViewController
  • How to take screen shot programmatically (Swift, SpriteKit)
  • Setting NavigationController's NavigationBar translucent property to false causes extra padding
  • Create multiple UIViews when the user taps the screen
  • Filter Array of [AnyObject] in Swift
  • How to add ParseUI to a swift project
  • Can Swift enums have multiple raw values?
  • Masking an image in Swift using CALayer and UIImage
  • KVC without NSObject (NSPredicate)
  • Any? to Dictionary Cast and retrieving value
  • NSCharacterSet.characterIsMember() with Swift's Character type
  • SCNScene: Calculate projected size of an object
  • How to use contains on a swiftyJSON object
  • Index of element in sorted()
  • How do I initialise a new NSDocument instance in Swift?
  • Multiple functions with the same name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org