ios Swift 中“未解析标识符的使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28996730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:09:54  来源:igfitidea点击:

'Use of Unresolved Identifier' in Swift

iosxcodeswift

提问by Apple Geek

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.

我正在使用 Swift 和 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

但是在 signedIn = false

I get the error:

我收到错误:

use of unresolved identifier "signedIn"

使用未解析的标识符“signedIn”

回答by lchamp

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

目标

回答by Cons Bulaquena

'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

当您忘记导入库时,Swift 中的“使用未解析的标识符”也会发生。例如我有错误: 在此处输入图片说明

In which I forgot the UIKit

其中我忘记了 UIKit

import UIKit

导入 UIKit

回答by Honey

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

对我来说,发生此错误是因为我试图调用嵌套函数。唯一的事情我不得不这样做有它固定为使功能的范围内它是可见的。

回答by snakeoil

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.

当重新启动 Xcode 不起作用时,这使我消失了。

回答by capikaw

Another place I've seen this error is when your project has multiple targetsAND 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.

一个很好的提示是查看左侧的“问题导航器”面板;根对象将显示发出投诉的目标。

回答by M Johnson

Your NewClassinherits from UIViewController. You declared signedInin ViewController. If you want NewClassto be able to identify that variable it will have to be declared in a class that your NewClassinherits from.

NewClass继承自UIViewController. 你signedInViewController. 如果您希望NewClass能够识别该变量,则必须在您NewClass继承的类中声明它。

回答by Fil0tti

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

您必须将此代码放在 NewClass 代码的末尾

回答by Vinod Supnekar

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

我在我的 Objective-Swift 项目中遇到了 Mantle Framework 的这个错误。我试过的是,

  1. Check if import is there in Bridging-Header.h file
  2. Change the Target Membership for Framework in Mantle.hfile as shown in below screenshot.
  1. 检查 Bridging-Header.h 文件中是否有导入
  2. Mantle.h文件中更改 Framework 的 Target Membership,如下面的屏幕截图所示。

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.hfile, you must get success.

在 Private Membership 之间切换首先构建项目,最终出现错误。然后为Mantle.h文件中出现的所有框架构建具有Public Membership的项目,您必须获得成功。

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

这只是在 Objective-C Swift 项目中使用多个框架构建的错误。

enter image description here

在此处输入图片说明

回答by Davron Jabborov

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

你忘记声明变量了。就放在var前面signedIn = false

回答by Ribaz

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

就我而言,我有一个 Object-C 文件,它也属于同一目标成员资格。我通过添加#import "YourObjectCFileHeader.h"内部文件来修复Bridging-Header.h