94

So I have been making an app, and everything has been working great. But today I made a new class like usual and for some reason in this class I can't access Public/Global variable from other classes. All the other classes can, but now when ever I try to make a new class I can't. How would this be fixed?

I am using Swift and Xcode 6.

Working Class:

import UIKit
import Foundation
import Parse
import CoreData

var signedIn = true

class ViewController: UIViewController {

New Class:

import UIKit

class NewClass: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        signedIn = false

}

But on signedIn = false

I get the error:

use of unresolved identifier "signedIn"

shim
  • 9,289
  • 12
  • 69
  • 108
Apple Geek
  • 1,121
  • 1
  • 10
  • 16

15 Answers15

298

One possible issue is that your new class has a different Target or different Targets from the other one.

For example, it might have a testing target while the other one doesn't. For this specific case, you have to include all of your classes in the testing target or none of them.

Targets

thomaux
  • 19,133
  • 10
  • 76
  • 103
lchamp
  • 6,592
  • 2
  • 19
  • 27
12

Once I had this problem after renaming a file. I renamed the file from within Xcode, but afterwards Xcode couldn't find the function in the file. Even a clean rebuild didn't fix the problem, but closing and then re-opening the project got the build to work.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
7

'Use of Unresolved Identifier' in Swift my also happen when you forgot to import a library. For example I have the error: enter image description here

In which I forgot the UIKit

import UIKit

oOEric
  • 1,069
  • 1
  • 10
  • 25
Cons Bulaquena
  • 2,083
  • 2
  • 26
  • 24
4

Sometimes the compiler gets confused about the syntax in your class. This happens a lot if you paste in source from somewhere else.

Try reducing the "unresolved" source file down to the bare minimum, cleaning and building. Once it builds successfully add all the complexity back to your class.

This has made it go away for me when re-starting Xcode did not work.

snakeoil
  • 497
  • 4
  • 12
4

Another place I've seen this error is when your project has multiple targets AND multiple bridging headers. If it's a shared class, make sure you add the resource to all bridging headers.

A good tip to is to look in the left Issue Navigator panel; the root object will show the target that is issuing the complaint.

capikaw
  • 12,232
  • 2
  • 43
  • 46
3

For me this error happened because I was trying to call a nested function. Only thing I had to do to have it fixed was to bring the function out to a scope where it was visible.

mfaani
  • 33,269
  • 19
  • 164
  • 293
3

In my case, I had an Object-C file which was also in the same Target Membership. I fixed by adding #import "YourObjectCFileHeader.h" inside file Bridging-Header.h

Ribaz
  • 476
  • 3
  • 10
1

Because you haven't declared it. If you want to use a variable of another class you must use

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var DestViewController : ViewController = segue.destinationViewController as ViewController
    DestViewController.signedIn = false
}

You have to put this code at the end of the NewClass code

Ftagliacarne
  • 675
  • 8
  • 16
  • yes but I have used other classes and they have worked fine without that, it's just when I make this new class. – Apple Geek Mar 11 '15 at 20:50
  • 2
    This won't work, he has the signedIn variable declared in the global scope, not inside his class. – Sam Mar 11 '15 at 20:57
1

Your NewClass inherits from UIViewController. You declared signedIn in ViewController. If you want NewClass to be able to identify that variable it will have to be declared in a class that your NewClass inherits from.

M Johnson
  • 11
  • 1
1

I got this error for Mantle Framework in my Objective-Swift Project. What i tried is ,

  1. Check if import is there in Bridging-Header.h file
  2. Change the Target Membership for Framework in Mantle.h file as shown in below screenshot.

Toggle between Private Membership first build the project , end up with errors. Then build the project with Public Membership for all the frameworks appeared for Mantle.h file, you must get success.

It is just a bug of building with multiple framework in Objective-C Swift project.

enter image description here

Vinod Supnekar
  • 143
  • 1
  • 7
0

If this is regarding a class you created, be sure that the class is not nested.

F.e

A.swift

class A {

   class ARelated {

   }
}

calling var b = ARelated() will give 'Use of unresolved identifier: ARelated'.

You can either:

1) separate the classes if wanted on the same file:

A.swift

class A {

}

class ARelated {

}

2) Maintain your same structure and use the enclosing class to get to the subclass:

var b = A.ARelated

htafoya
  • 18,261
  • 11
  • 80
  • 104
0

I did a stupid mistake. I forgot to mention the class as public or open while updating code in cocoapod workspace.

Please do check whether accesor if working in separate workspace.

abhimanyu jindal
  • 658
  • 8
  • 18
0

You forgot to declare the variable. Just put var in front of signedIn = false

shim
  • 9,289
  • 12
  • 69
  • 108
  • I would assume that that is just a typo in the posted code (along with the missing closing bracket on `viewDidLoad`. – shim Oct 10 '18 at 16:15
0

My issue was calling my program with the same name as one of its cocoapods. It caused a conflict. Solution: Create a program different name.

0

This is not directly to your code sample, but in general about the error. I'm writing it here, because Google directs this error to this question, so it may be useful for the other devs.


Another use case when you can receive such error is when you're adding a selector to method in another class, eg:

private class MockTextFieldTarget {
private(set) var didCallDoneAction = false
    @objc func doneActionHandler() {
        didCallDoneAction = true
    }
}

And then in another class:

final class UITextFieldTests: XCTestCase {
    func testDummyCode() {
        let mockTarget = MockTextFieldTarget()
        UIBarButtonItem(barButtonSystemItem: .cancel, target: mockTarget, action: MockTextFieldTarget.doneActionHandler)
        // ... do something ...
    }
}

If in the last line you'd simply call #selector(cancelActionHandler) instead of #selector(MockTextFieldTarget.cancelActionHandler), you'd get

use of unresolved identifier

error.

Nat
  • 12,032
  • 9
  • 56
  • 103