传递字符串有时会迅速导致崩溃

时间:2014-11-06 10:37:24

标签: ios swift xcode6

我遇到了一个奇怪的事情我正在崩溃这个功能:

func addQuestion(text:String!)
{
    if let txt=text
    {
        let question=PFObject(className: "questions")
        question["text"]=txt
        question["userAdding"]=PFUser.currentUser()
        question["answeredCount"]=0
        question.setObject([], forKey: "answers")
        question.setObject(NSDate(), forKey: "dateAdded")
        question.setObject(Int(arc4random()) % 10, forKey: "random")
        if debug
        {question.saveInBackgroundWithBlock(nil)}
        else
        {question.saveEventually()}
    }

}

我在#34中遇到了崩溃;如果让txt = text" 这是一个回溯:

thread #1: tid = 0x241c0c, 0x00063470 pytajnik`pytajnik.ApiClient.addQuestion (text=Some, self=(ObjectiveC.NSObject = {}, debug = true))(Swift.ImplicitlyUnwrappedOptional<Swift.String>) -> () + 10796 at ApiClient.swift:32, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
frame #0: 0x00063470 pytajnik`pytajnik.ApiClient.addQuestion (text=Some, self=(ObjectiveC.NSObject = {}, debug = true))(Swift.ImplicitlyUnwrappedOptional<Swift.String>) -> () + 10796 at ApiClient.swift:32
frame #1: 0x00035428 pytajnik`pytajnik.AddQuestionViewController.addQuestion (sender=AnyObject at 0x006ca054, self=0x17ef85d0)(Swift.AnyObject) -> () + 796 at AddQuestionViewController.swift:17
frame #2: 0x00035a6c pytajnik`@objc pytajnik.AddQuestionViewController.addQuestion (pytajnik.AddQuestionViewController)(Swift.AnyObject) -> () + 100 at AddQuestionViewController.swift:0
frame #3: 0x2a5fdc2a UIKit`-[UIApplication sendAction:to:from:forEvent:] + 70
frame #4: 0x2a5fdbd0 UIKit`-[UIControl sendAction:to:forEvent:] + 44
...

问题是,应用程序始终没有崩溃。有时它有时会起作用。我无法找到一种模式。 有人能帮我理解什么是错的吗?

更新

thread #1: tid = 0x243f69, 0x0005d478 pytajnik`pytajnik.ApiClient.addQuestion (text=Some, self=(ObjectiveC.NSObject = {}, debug = true))(Swift.Optional<Swift.String>) -> () + 9820 at ApiClient.swift:35, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
frame #0: 0x0005d478 pytajnik`pytajnik.ApiClient.addQuestion (text=Some, self=(ObjectiveC.NSObject = {}, debug = true))(Swift.Optional<Swift.String>) -> () + 9820 at ApiClient.swift:35
frame #1: 0x0002f80c pytajnik`pytajnik.AddQuestionViewController.addQuestion (sender=AnyObject at 0x006c4054, self=0x166a5560)(Swift.AnyObject) -> () + 1760 at AddQuestionViewController.swift:17
frame #2: 0x0002fe40 pytajnik`@objc pytajnik.AddQuestionViewController.addQuestion (pytajnik.AddQuestionViewController)(Swift.AnyObject) -> () + 100 at AddQuestionViewController.swift:0
frame #3: 0x2a5fdc2a UIKit`-[UIApplication sendAction:to:from:forEvent:] + 70
frame #4: 0x2a5fdbd0 UIKit`-[UIControl sendAction:to:forEvent:] + 44
frame #5: 0x2a5e8862 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 582
frame #6: 0x2a5fd63c UIKit`-[UIControl touchesEnded:withEvent:] + 588
frame #7: 0x2a5fd316 UIKit`-[UIWindow _sendTouchesForEvent:] + 522
frame #8: 0x2a5f6be0 UIKit`-[UIWindow sendEvent:] + 544

2 个答案:

答案 0 :(得分:0)

您将函数参数声明为隐式解包的可选!,这意味着您确定它永远不会是nil

如果此参数可能nil可能将其声明为可选:

func addQuestion(text: String?) {
    if let txt = text {
        ...
    }
}

答案 1 :(得分:0)

您已将text参数声明为隐式解包的可选项,如果text为零,则会导致应用崩溃,这就是您的情况。

除非您有特定的理由使用隐式展开的可选项,否则最好使用可选的String?或非可选的String,如果参数永远不能为零。

所以要解决问题,只需将功能签名更改为:

func addQuestion(text:String?)

建议阅读:Implicitly Unwrapped Optionals