ios – NSInvalidArgumentException – Attempt to insert nil object from objects

[ad_1]

I had the error below:

'*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]'

it happens when I try to save texts from textfields using saveInBackground or other kind of saving method of Parse. Because code is little, all code is pasted below:

@IBAction func onSave(_ sender: Any) {
    let profile = PFObject(className: "Info")

    profile["preferredName"] = nameTextField.text!
    profile["major"] = majorTextField.text!
    profile["introduction"] = bioTextView.text!

    let segmentIndex = yearSegmented.selectedSegmentIndex
    UserDefaults.standard.set(segmentIndex, forKey: "segmentIndex")
    profile["year"] = yearSegmented.titleForSegment(at: segmentIndex)!

    print(profile.allKeys) // print keys in profile
    print(profile.value(forKey: "introduction")!, profile.value(forKey: "major")!,
    profile.value(forKey: "year")!, profile.value(forKey: "preferredName")!) // print values in profile
    
    profile.saveInBackground(block: { (success, error) in // line where the error happens
        if success {
            UserDefaults.standard.set(true, forKey: "hasProfile")
            print("profile saved")
        } else {
            print("failed to save profile")
        }
    })

This is how Parse IOS Guide told me about how to save objects:

let gameScore = PFObject(className:"GameScore")
gameScore["score"] = 1337
gameScore["playerName"] = "Sean Plott"
gameScore["cheatMode"] = false
gameScore.saveInBackground { (succeeded, error)  in
if (succeeded) {
    // The object has been saved.
} else {
    // There was a problem, check error.description
}

}

I tested if any content in profile is nil, but all keys and values are there and can be printed. I don’t see anything nil here, so I cannot understand this error. If anyone could shed some light to enlighten me, I would be be very grateful for this.

In case anyone needs the stack trace:

0   CoreFoundation                      0x00000001803f25e4 __exceptionPreprocess + 236
1   libobjc.A.dylib                     0x000000018019813c objc_exception_throw + 56
2   CoreFoundation                      0x00000001804775b4 -[__NSCFString characterAtIndex:].cold.1 + 0
3   CoreFoundation                      0x0000000180474fc4 -[__NSPlaceholderArray initWithCapacity:].cold.1 + 0
4   CoreFoundation                      0x00000001802ef138 -[__NSPlaceholderArray initWithObjects:count:] + 184
5   CoreFoundation                      0x00000001803dddec +[NSArray arrayWithObjects:count:] + 44
6   Parse                               0x0000000104eda478 -[PFTaskQueue enqueue:] + 380
7   Parse                               0x0000000104e78b2c -[PFObject saveInBackground] + 148
8   Parse                               0x0000000104e78c1c -[PFObject saveInBackgroundWithBlock:] + 68
9   StudyMate                           0x00000001041c59bc $s9StudyMate18EditViewControllerC6onSaveyyypF + 7276
10  StudyMate                           0x00000001041c5fe4 $s9StudyMate18EditViewControllerC6onSaveyyypFTo + 68
11  UIKitCore                           0x0000000184d81fa0 -[UIApplication sendAction:to:from:forEvent:] + 96
12  UIKitCore                           0x00000001842a7714 -[UIBarButtonItem _triggerActionForEvent:] + 176
13  UIKitCore                           0x000000018427fe24 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 36
14  UIKitCore                           0x000000018427fcd8 -[_UIButtonBarTargetAction _invoke:forEvent:] + 168
15  UIKitCore                           0x0000000184d81fa0 -[UIApplication sendAction:to:from:forEvent:] + 96
16  UIKitCore                           0x0000000184680bd8 -[UIControl sendAction:to:forEvent:] + 124
17  UIKitCore                           0x0000000184680fc0 -[UIControl _sendActionsForEvents:withEvent:] + 352
18  UIKitCore                           0x0000000184681010 -[UIControl _sendActionsForEvents:withEvent:] + 432
19  UIKitCore                           0x000000018467f8e8 -[UIControl touchesEnded:withEvent:] + 516
20  UIKitCore                           0x0000000184dc0af0 -[UIWindow _sendTouchesForEvent:] + 1104
21  UIKitCore                           0x0000000184dc25d4 -[UIWindow sendEvent:] + 4332
22  UIKitCore                           0x0000000184d9a390 -[UIApplication sendEvent:] + 784
23  UIKitCore                           0x0000000184e286f4 __dispatchPreprocessedEventFromEventQueue + 7520
24  UIKitCore                           0x0000000184e2a770 __processEventQueue + 6764
25  UIKitCore                           0x0000000184e22760 __eventFetcherSourceCallback + 184
26  CoreFoundation                      0x0000000180360820 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
27  CoreFoundation                      0x0000000180360720 __CFRunLoopDoSource0 + 204
28  CoreFoundation                      0x000000018035fab0 __CFRunLoopDoSources0 + 256
29  CoreFoundation                      0x000000018035a004 __CFRunLoopRun + 744
30  CoreFoundation                      0x0000000180359804 CFRunLoopRunSpecific + 572
31  GraphicsServices                    0x000000018c23660c GSEventRunModal + 160
32  UIKitCore                           0x0000000184d7bd2c -[UIApplication _run] + 992
33  UIKitCore                           0x0000000184d808c8 UIApplicationMain + 112
34  libswiftUIKit.dylib                 0x00000001b6766224 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 100
35  StudyMate                           0x00000001041c7a9c $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
36  StudyMate                           0x00000001041c7a24 $s9StudyMate11AppDelegateC5$mainyyFZ + 44
37  StudyMate                           0x00000001041c7b20 main + 28
38  dyld                                0x0000000104501cd8 start_sim + 20
39  ???                                 0x0000000104451088 0x0 + 4366602376
40  ???                                 0xb23a000000000000 0x0 + 12842577287400390656

[ad_2]

Source link

Leave a Reply

Your email address will not be published.