0

I'm trying to send an http request to the server where I need to send the following required parameters

description:string, amount: float, occurred_at: string,location: string

As the amount I need to pass it through a textfield (the other fields are coming through an API) has been made a Segue passing the PreObject object to that view whose amount is needed.

Magical Record is using for objects

PreObject

( (entity: PreObject; id: 0xd000000000040006 ; data: { amount = nil; creatingOnBackend = 1; currency = nil; description = ""; expense = nil; extraInformation = "0xd000000001400010 "; identifier = 47; ignoredAt = nil; location = nil; occuredAt = "2017-03-12 00:00:00 +0000"; participants = ""; payer = nil; receiver = nil; status = nil; tags = ""; }))

Http Request requisition:

 class  func sendToBackend(object: PreObject,preObjectID: NSNumber, withCompletion completion: (error: NSError?) -> ()) {

        let url = "\(ServicesUtils.baseUrl())/objects/\(preObjectID)/send"
        let params = [object.dictionaryBackend()]
        let sessionManager = ServicesUtils.rexpenseBaseSessionManager()
        let request = sessionManager.requestSerializer.requestWithMethod("POST", URLString: url, parameters: params, error: nil)


        let task = sessionManager.dataTaskWithRequest(request) { urlResponse, responseObject, error in
            if let _ = error {
                let returnError = ServicesUtils.rexpenseHandleErrorWithResponse(urlResponse, andRequest: request, andResult: responseObject)
                completion(error: returnError)
                return

            }

            guard let dict = responseObject as? [String: AnyObject] else {
                let errorMessage = "Error"
                let error = NSError(domain: "xxxxxxxxx", code: -1, userInfo: [NSLocalizedDescriptionKey : errorMessage])
                completion(error: error)
                return


            }

            let moc = NSManagedObjectContext.MR_defaultContext()
            PreObject.createPreObjectwithDictionary(dict, inContext: moc)
            moc.MR_saveToPersistentStoreAndWait()
            completion(error: nil)
        }
        task.resume()
    }

Dictionary that pass in parameters of requisition:

- (NSMutableDictionary *)dictionaryBackend {

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    dict[@"description"] = description == nil ? [NSNull null] : description;
    dict[@"payer"] = @{@"id": self.payer.identifier,
                       @"type": @"xxxxx"};

    if ([self.amount doubleValue] == [self.amount intValue]) {
        NSNumber *number = [NSNumber numberWithDouble:[self.amount doubleValue] + 0.001];
        dict[@"amount"] = number;

    } else {
        dict[@"amount"] = self.amount;
    }

    NSDateFormatter *dateFormatter = [ServicesUtils defaultDateFormatter];
    dict[@"occurred_at"] = [dateFormatter stringFromDate:self.occuredAt];

    dict[@"tags"] = [self tagsSeparatedByComma];
    dict[@"location"] = self.address == nil ? [NSNull null] : self.address;
    dict[@"currency"] = self.currency;

           return dict;

When i tap in UIButton app crash and show this error on debug area:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

Someone can help me?

1 Answer 1

0

This crash is generally caused by trying to create a dictionary with nil values.

Check before the line:

dict[@"payer"] = @{@"id": self.payer.identifier,
                   @"type": @"xxxxx"};

if self.player.identifier is nil.

I cannot advice what you should when it is nil, because that can mean different things in different applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.