2015-09-02 242 views
1

我是swift的新手。我想在靜態函數中使用無靜態成員的Swift類以下是我的代碼。我無法在靜態函數中訪問靜態類的成員。有沒有辦法在swift函數中訪問非靜態成員?在swift類的靜態函數中訪問非靜態常量

public class Test{ 
private let testString:String 

init(test:String){ 
    testString = test 
} 

static func Get(url:String){ 
//Here testString is not accessable.. 
} 
} 

回答

4

不,沒有辦法訪問靜態函數中的非靜態變量。不過,您可以創建該類的靜態實例(一種單例生成方式)並訪問它的testString變量。