Can't access singleton instance from Swift framework

26 views
Skip to first unread message

Tron Thomas

unread,
May 2, 2015, 1:32:17 PM5/2/15
to swift-l...@googlegroups.com
Using Xcode 6.3, I created a very simple, contrived command line tool in Swift. It contains three modules.

The main module:

import Foundation

let displayer
= ValueDisplayer()

displayer
.displayValue()


A ValueDisplayer module:

import Foundation

class ValueDisplayer {
    func displayValue
() {
        println
("The value is \(ValueProvider.instance.value)")
   
}
}



and a ValueProvider module:

import Foundation

public class ValueProvider {
   
class var instance: ValueProvider {
       
struct Static {
           
static let instance = ValueProvider()
       
}
       
       
return Static.instance
   
}
   
   
var value: Int {
       
return Int(arc4random())
   
}
}



This all compiled and ran successfully. However, I then decided to convert the ValueProvider to a framework. I created a Cocoa Framework target, and moved the ValueProvider module into it instead of in the command line target. I modified the ValueDisplayer module to this:

import Foundation
import ValueProvider

class ValueDisplayer {
    func displayValue
() {
        println
("The value is \(ValueProvider.instance.value)")
   
}
}



and configured the command line tool to link against the framework. Now I get the following compilation error in the ValueDisplayer module related to the println statement:

Module 'ValueProvider' has no member named 'instance'

I'm confused as to why the code doesn't work anymore. I'm wondering if the ValueProvider class is not being qualified correctly anymore, only it is unclear how that should be done.

What is needed to allow the command line tool to compile by linking against the framework?

Jim Dovey

unread,
May 2, 2015, 1:47:57 PM5/2/15
to Tron Thomas, swift-l...@googlegroups.com
You need to mark the "instance" property as being public. Right now it's only internal. Note that making a class public doesn't automatically make its members public as well.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3fa62b93-cadc-4dfb-818d-1b6c388f8ad9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tron Thomas

unread,
May 2, 2015, 4:06:58 PM5/2/15
to Jim Dovey, swift-l...@googlegroups.com
I tried making the instance variable public, and I’m still getting the same compile error.

Jim Dovey

unread,
May 2, 2015, 4:21:53 PM5/2/15
to Tron Thomas, swift-l...@googlegroups.com
Reading through it again, there may be a scoping problem here. Is your framework/module called ValueProvider, the same as the class? If that is the case, then it sounds as though simply using "ValueProvider" unqualified is being treated as the module, not the class.

Note that the error says "Module 'ValueProvider'", not "Class 'ValueProvider'".

To test the hypothesis, use the fully-qualified name of the class. Replace "ValueProvider.instance" with "ValueProvider.ValueProvider.instance". If that solves the problem, then I would suggest renaming your module to avoid the name clash. When the compiler looks for ValueProvider then, it will not find a top-level symbol (module name) so will search within the imported modules and find your class.

Sent from my iPhone

Tron Thomas

unread,
May 2, 2015, 5:53:54 PM5/2/15
to Jim Dovey, swift-l...@googlegroups.com
The name of the framework in also ValueProvider. Theoretically that shouldn’t matter. Someone should be able to call their framework whatever they want. I did notice that it said ‘Module’ instead of ‘Class’, which is why I originally mentioned qualifying the class. Trying ValueProvider.ValueProvider.instance results in the error: Module ‘ValueProvider’ has no member named ‘ValueProvider’.

I’m having great difficulty understanding what it takes to properly reference items in the frame work. I even tried adding a global function to the framework, and was unable to figure out how to successfully call that as well.

You received this message because you are subscribed to a topic in the Google Groups "Swift Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swift-language/lyxBy6mKA68/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swift-languag...@googlegroups.com.

To post to this group, send email to swift-l...@googlegroups.com.

Tron Thomas

unread,
May 2, 2015, 8:01:46 PM5/2/15
to Jim Dovey, swift-l...@googlegroups.com
I got program to build and run. What I had to do was go to the Build Phases for the framework and remove the generated header file from the Headers section. I don’t know why this makes things work. My understanding is that header is needed to help with Objective-C interaction. If the program had needed to  interact with Objective-C as well as Swift, I’m wondering how anyone could get things to work properly.

Reply all
Reply to author
Forward
0 new messages