Home > DeveloperSection > Forums > How to call a function within a structure and a class?
Ashish Pandey

Total Post:128

Points:898
Posted on    October-13-2014 1:12 AM

 iPhone iPhone  Mobile Development 
Ratings:


 2 Reply(s)
 613  View(s)
Rate this:
I have two files: ClassA.swift and ClassB.swift

class ClassA: NSObject {

        struct StructA {

            func talk(string: String) {

                println("I say: \(string)")

            }

        }

    }

 

 

    class ClassB: NSObject {

        func makeItTalk(string: String) {

            ClassA.StructA.talk("Hello") <--------------

        }

    }

I get the following error message

Type 'ClassA.StructA' does not conform to protocol 'StringLiteralConvertible'

Any idea why? Is it actually something than can be achieved?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Supported
Posted on    October-13-2014 1:14 AM

You are accessing StructA in a static context, whereas you need an instance of it:

class ClassB: NSObject {

        func makeItTalk(string: String) {

            var myStruct = ClassA.StructA()

            myStruct.talk("Hello")

        }

    }

Alternatively, if your goal is to access that method statically, just declare it as a static struct method:

static func talk(string: String) {

        println("I say: \(string)")

    }


Pawan Shukla

Total Post:37

Points:267
Posted on    October-13-2014 1:15 AM

You are using talk as static method, but it is declared as instance method. Change to:

class ClassA: NSObject {

        struct StructA {

            static func talk(string: String) {

                println("I say: \(string)")

            }

        }

    }


Don't want to miss updates? Please click the below button!

Follow MindStick