菜鸟教程小白 发表于 2022-12-11 18:30:52

ios - 为什么是 "Unknown class"?


                                            <p><p>我是编程初学者,也是 Stack Overflow 的新手。我还在弄清楚代码和这个网站是如何工作的,所以如果我没有正确使用它们,我很抱歉。 (我的母语不是英语,但我会尽我所能解释我的问题)</p>

<p>我使用的是 Xcode 8.1 版和 Swift。</p>

<p>我正在为我的学校项目构建一个应用程序。我已经创建了 To-Do-List App 和 Countdown Timer App。所以,我现在想把它们放在一个应用程序中。</p>

<p>我创建了一个新项目并插入了标签栏 Controller 。然后复制我在 To-Do-List 和 Countdown Timer 应用中使用的所有文件,并设置 Storyboard。</p>

<p>没有警告标记,但是当我运行模拟器时,出现错误并且模拟器停止。我得到的错误是 AppDelegate.swift 中这一行中的“线程 1:信号 SIGABRT”:</p>

<pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
</code></pre>

<p>控制台中的消息是:</p>

<pre><code>2017-01-06 20:32:22.207 PP Final App Unknown class PomodoroViewController in Interface Builder file.
2017-01-06 20:32:22.713 PP Final App *** Terminating app due to uncaught exception &#39;NSUnknownKeyException&#39;, reason: &#39;[&lt;UIViewController 0x7fd7faf075d0&gt; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lbTimer.&#39;
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001100c234b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010fb2321e objc_exception_throw + 48
    2   CoreFoundation                      0x00000001100c2299 - + 9
    3   Foundation                        0x000000010f63326f - + 291
    4   UIKit                               0x000000011067f4ef - + 88
    5   UIKit                               0x00000001108f379e - + 109
    6   CoreFoundation                      0x0000000110067590 - + 256
    7   UIKit                               0x00000001108f2122 - + 1867
    8   UIKit                               0x0000000110685c21 - + 386
    9   UIKit                               0x0000000110686543 - + 177
    10UIKit                               0x0000000110686878 - + 201
    11UIKit                               0x00000001106870cc - + 27
    12UIKit                               0x00000001106e52df - + 483
    13UIKit                               0x00000001106e4721 - + 59
    14UIKit                               0x00000001106e05e2 - + 365
    15UIKit                               0x00000001106e0464 - + 234
    16UIKit                               0x000000011059e6e6 + + 90
    17UIKit                               0x00000001106daa00 - + 354
    18UIKit                               0x00000001106dbb7a - + 206
    19UIKit                               0x000000011068ca0f - + 692
    20UIKit                               0x000000011068d11f - + 147
    21UIKit                               0x000000011068e913 - + 507
    22UIKit                               0x0000000110595151 - + 621
    23UIKit                               0x00000001105a5cf0 - + 451
    24UIKit                               0x00000001105947a1 - + 838
    25UIKit                               0x0000000110550f5b - + 849
    26UIKit                               0x00000001105513a2 - + 293
    27UIKit                               0x0000000110564cb5 - + 42
    28UIKit                               0x00000001104ddc89 - + 4818
    29UIKit                               0x00000001104e3de9 - + 1731
    30UIKit                               0x00000001104e0f69 - + 188
    31FrontBoardServices                  0x0000000113ed3723 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    32FrontBoardServices                  0x0000000113ed359c - + 189
    33FrontBoardServices                  0x0000000113ed3925 - + 45
    34CoreFoundation                      0x0000000110067311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    35CoreFoundation                      0x000000011004c59c __CFRunLoopDoSources0 + 556
    36CoreFoundation                      0x000000011004ba86 __CFRunLoopRun + 918
    37CoreFoundation                      0x000000011004b494 CFRunLoopRunSpecific + 420
    38UIKit                               0x00000001104df7e6 - + 434
    39UIKit                               0x00000001104e5964 UIApplicationMain + 159
    40PP Final App                        0x000000010f52c1cf main + 111
    41libdyld.dylib                     0x000000011373c68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
</code></pre>

<p>在此消息中,我看到“Interface Builder 文件中的未知类 PomodoroViewController”。这就是问题所在,对吧?但我不知道为什么我收到这条消息。文件有问题吗?</p>

<p>我使用 Tab Bar Controller 的方式对吗?</p>

<p>这是番茄钟 ViewController 的代码文件:</p>

<p>番茄 ViewController </p>

<pre><code>import UIKit

class PomodoroViewController: UIViewController {

    @IBOutlet weak var lbTimer: UILabel!

    let pomodoroTime: TimeInterval = 60 * 25 //Pomodoro Timer 25 minutes
    let formatter = DateFormatter()
    var theTime: TimeInterval = 0.0

    override func viewDidLoad(){
      super.viewDidLoad()
      formatter.dateFormat = &#34;mm:ss&#34;
      let startTime = Date(timeIntervalSinceReferenceDate: pomodoroTime)
      lbTimer.text = formatter.string(from: startTime)
    }

    override func didReceiveMemoryWarning(){
      super.didReceiveMemoryWarning()
    }

    @IBAction func countDown(_ sender: UIButton) {
      theTime = pomodoroTime
      Timer.scheduledTimer(timeInterval: 1.0,
                           target: self,
                           selector: #selector(PomodoroViewController.tickTimer(timer:)),
                           userInfo: nil,
                           repeats: true)
    }

    func tickTimer(timer: Timer){
      theTime -= 1.0
      let newTime = Date(timeIntervalSinceReferenceDate: theTime)
      if theTime &lt; 0.1 {
            timer.invalidate()
      }
    }

}
</code></pre>

<p> Storyboard是这样的:
<a href="/image/0apjG.png" rel="noreferrer noopener nofollow">storyboard</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这个错误的意思是:</p>

<ol>
<li>你有一个 Storyboard(或者可能是一个 xib),它说其中一个 ViewController 是 <code>PomodoroViewController</code>。但是...</li>
<li><code>PomodoroViewController</code> 应用运行时不存在。</li>
</ol>

<p>消息说 iOS 试图找到 <code>PomodoroViewController</code> 但它不存在,因此它尝试使用 <code>UIViewController</code> 代替。除了 <code>UIViewController</code> 没有 <code>lbTimer</code> 属性,这会导致应用崩溃。</p>

<p>发生这种情况的原因有几个:</p>

<ul>
<li>您可能忘记将 <code>PomodoroViewController</code> 复制到新项目中。</li>
<li>如果您确实复制了它,您可能没有将它包含在应用目标中。 [未编译的文件可以包含在 Xcode 项目中,因为您可能将同一个项目文件用于多个目标。]</li>
</ul>

<p>在第二种情况下,确保在 Xcode 窗口右侧的文件检查器中检查了该文件:</p>

<p> <a href="/image/Ztd0J.png" rel="noreferrer noopener nofollow"><img src="/image/Ztd0J.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么是&#34;Unknown class&#34;?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41513564/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41513564/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么是 &#34;Unknown class&#34;?