stevengbu stevengbu - 2 months ago 8
iOS Question

Use of unresolved identifier 'Static'

I have updated xcode 8.0 swift 3 and I found many errors. This is one of them :


Use of unresolved identifier 'Static'


This is my class that i created and worked in previous version xcode 7.3.1 swift 2.

import UIKit

enum FONTSIZE:Int
{
case sizesmall = 1
case sizesbig = 2
case sizemedium = 3
}

class sizeFont: NSObject {

private static var __once: () = {
Static.instance = sizeFont()
}()

class func getSize(_ enumFont : FONTSIZE) -> CGFloat {
var siz = 17
switch(enumFont){
case .sizesbig:
if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 0){// kecil
if(isphone()){
siz = 22//17
}else{
siz = 22//24
}

}else if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 1){// besar
if(isphone()){
siz = 30//19
}else{
siz = 30//28
}
}else{
if(isphone()){
siz = 22//17
}else{
siz = 22//22
}
}

break
case .sizesmall:
if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 0){// kecil
if(isphone()){
siz = 17//15//12
}else{
siz = 24//22//19
}
}else if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 1){// besar
if(isphone()){
siz = 19//17//15
}else{
siz = 28//26//24
}
}else{
if(isphone()){
siz = 17//15//12
}else{
siz = 24//22//19
}
}
break
case .sizemedium:
if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 0){// kecil
if(isphone()){
siz = 15
}else{
siz = 22
}
}else if((UserDefaults.standard.value(forKey: "fontsize") as AnyObject).int32Value == 1){// besar
if(isphone()){
siz = 17
}else{
siz = 26
}
}else{
if(isphone()){
siz = 15
}else{
siz = 22
}
}
break
}
return CGFloat(siz)
}

func getnametag(){

}

class func isphone() ->Bool {
if(UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone){
return true
}else{
return false
}
}

// example singleton swift
class var sharedInstance: sizeFont {
struct Static {
static var onceToken: Int = 0
static var instance: sizeFont? = nil
}
_ = sizeFont.__once
return Static.instance!
}


}


The red line code is Static.instance = sizeFont().

I am not sure why I am getting this, does anyone else know ?

Answer

Static is declared inside class variable, that cause unreachability to everybody outside var declaration, just move it to outside.

...
struct Static {
    static var onceToken: Int = 0
    static var instance: sizeFont? = nil
}
// example singleton swift
class var sharedInstance: sizeFont {
    _ = sizeFont.__once
    return Static.instance!
}
...