AFNetworking - Build NSDictionary parameters - iphone

I'm trying to build my NSDictionnary to send to a request using the AFNetworking framework, but it seems that I'm quite confused about how to do it properly.
Here's what the server is expecting :
{
"limit":10,
"filters":
[
{"field":"owner","operator":"EQUAL","value":"ownerId","type":"integer"},
{"field":"date","operator":"GE","value":"30 Jun 2010 00:00:00","type":"date"},
],
"order":[{"field":"date","order":"ASC"}],
"page":0
}
What I'm trying to do (I don't really know if it's the right way to do it tbh), is to build a NSDictionary like the following :
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
#"10", #"chunkSize",
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:#"owner", #"field", #"EQUAL", #"operator", #"ownerId", #"value", #"integer", #"type", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"GE", #"operator", #"30 Jun 2010 00:00:00", #"value", #"date", #"type", nil],
nil], #"filters",
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"ASC", #"order", nil],
nil], #"order",
#"0", #"page",
nil];
But I have the following error when the view is loading :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil
I know I screw up building the parameters properly, but I can't manage to do it after several tries. Could anyone help ? Moreover, I don't really know the differences that I must implement here with the [] and the {}. I read that {} was for a dictionary and [] for an array, but I don't really see how to translate it in my case.

Your mistake is that the value for the dictionary starting on line 3 needs to be wrapped in an array.
At least until Objective-C array and hash literals go mainstream, my preferred method for creating complex dictionaries is to build them from an NSMutableDictionary. In your case:
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setValue:#"10" forKey:#"limit"];
// ...
NSMutableArray *mutableFilters = [NSMutableArray array];
NSMutableDictionary *mutableOwnerFilterDictionary = [NSMutableDictionary dictionary];
[mutableOwnerFilterDictionary setValue:#"owner" forKey:#"field"];
// ...
[mutableFilters addObject:mutableOwnerFilterDictionary];
[mutableParameters setValue:mutableFilters forKey:#"filters"];
// ...
Also, be sure you're sending that over as JSON by setting AFJSONParameterEncoding to your AFHTTPClient.

The brackets [] signify an array, while the braces {} signify an object (dictionary in this context). To produce the structure you require:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
#"10", #"chunkSize",
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:#"owner", #"field", #"EQUAL", #"operator", #"ownerId", #"value", #"integer", #"type", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"GE", #"operator", #"30 Jun 2010 00:00:00", #"value", #"date", #"type", nil],
nil], #"filters",
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"ASC", #"order", nil],
nil], #"order",
#"0", #"page",
nil];

Related

How to crop video into square iOS with AVAssetWriter

I'm using AVAssetWriter to record a video and I want to be able to crop the video into a square with a offset from the top. Here is my code -
NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
#320, AVVideoCleanApertureWidthKey,
#320, AVVideoCleanApertureHeightKey,
#10, AVVideoCleanApertureHorizontalOffsetKey,
#10, AVVideoCleanApertureVerticalOffsetKey,
nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
#3, AVVideoPixelAspectRatioHorizontalSpacingKey,
#3,AVVideoPixelAspectRatioVerticalSpacingKey,
nil];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
#1,AVVideoMaxKeyFrameIntervalKey,
videoCleanApertureSettings, AVVideoCleanApertureKey,
//AVVideoScalingModeFit,AVVideoScalingModeKey,
videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
nil];
NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
codecSettings,AVVideoCompressionPropertiesKey,
#320, AVVideoWidthKey,
#320, AVVideoHeightKey,
nil];
Whenever I uncomment the AVVideoScalingModeKey, the my assetWriter gives me an error about not being able to apply the videoCompressionSettings. I tried using How do make a reduced size video using AVAssetWriter? but it still didn't work for me.
I added the AVVideoScalingModeFit,AVVideoScalingModeKey to my videoCompressionSettings to get it to work.

printing values of keys in NSDictionary

What I am having so far right now is
NSArray *keys = [NSArray arrayWithObjects:#"firstName",#"lastName",#"phoneNumber",#"email",#"password",nil];
NSArray *objects = [NSArray arrayWithObjects:#"nil",#"nil",#"nil",#"nil",#"abc",nil];
dictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];
NSLog(#"pass is %#",[keys objectAtIndex:4]);
NSLog(#"value of pass is%#",[dictionary objectForKey:#"password"]);
However, What I got from the debugger is
pass is password
value of pass is (null)
Can anyone explain why the value is null.It should be abc,shouldn't it.
The following line would have given a warning:
dictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];
It should read:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
Change this line you are missing s
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
you can do it in the fallowing way also...
NSMutableDictionary *dictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:#"nil",#"firstName",#"nil",#"lastName",#"nil",#"phoneNumber",#"nil",#"email",#"abc",#"password", nil];
NSLog(#"value of pass is%#",[dictionary objectForKey:#"password"]);

How can I use NSDictionary's methods to add objects and keys?

I want to make an NSDictionary that has the following values where symbol & id are my keys.
I want to map "AAPL" to symbol
I want to map 100 to id.
"AAPL":symbol
"100":id
"GOOG":symbol
"101":id
"YHOO":symbol
"102":id
How can I create a dictionary of these values with
dictionaryWithObjectsAndKeys
Which one is key, which one is value? Assuming you want map symbol to id, then
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
#"100", #"AAPL", // 100 is object, AAPL is key
#"101", #"GOOG",
#"102", #"YHOO",
// etc...
nil];
If the IDs are numbers, use
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 100], #"AAPL",
// etc...
nil];
If, OTOH, you want to map ids to symbols, then reverse the entries:
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
#"AAPL", #"100",
#"GOOG", #"101"
// etc...
nil];
The data you describe would most likely be represented by an array of dictionaries, since the keys would be repeated for each entry.
NSArray *array = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:#"APPL", #"symbol", #"100", #"id", nil], [NSDictionary dictionaryWithObjectsAndKeys:#"GOOG", #"symbol", #"101", #"id", nil], [NSDictionary dictionaryWithObjectsAndKeys:#"YHOO", #"symbol", #"103", #"id", nil], nil];
Assuming you want to use the ids as the keys and the symbols as the values, this should work:
NSDictionary *stocks = [NSDictionary dictionaryWithObjectsAndKeys:#"AAPL", #"100", #"GOOG", #"101", #"YHOO", #"102", nil];

Create an NSDictionary of NSDictionaries

Believe it or not, I have searched the internet before asking this question. Unbelievably, I have yet to find a nice clear example of how to create an NSDictionary of NSDictionaries.
Here is my code so far, but it prints null. Any ideas?
// Here I am creating the dictionaries in the code until I start getting them from the server ;)
NSArray *keys = [NSArray arrayWithObjects:#"mission", #"target", #"distance",#"status", nil];
NSArray *objectsA = [NSArray arrayWithObjects:#"tiger", #"bill", #"5.4km", #"unknown", nil];
NSDictionary *tiger = [NSDictionary dictionaryWithObjects:objectsA
forKeys:keys];
NSArray *objectsB = [NSArray arrayWithObjects:#"bull", #"roger", #"10.1km", #"you are dead", nil];
NSDictionary *bull = [NSDictionary dictionaryWithObjects:objectsB
forKeys:keys];
NSArray *objectsC = [NSArray arrayWithObjects:#"peacock", #"geoff", #"1.4km", #"target liquidated", nil];
NSDictionary *peacock = [NSDictionary dictionaryWithObjects:objectsC
forKeys:keys];
// activeMissions = [NSArray arrayWithObjects:tiger, bull, peacock, nil];
[activeMissions setObject:tiger forKey:#"tiger"];
[activeMissions setObject:bull forKey:#"bull"];
[activeMissions setObject:peacock forKey:#"peacock"];
NSLog(#"active Missions %#", activeMissions);
You are not intializing activeMissions, that is why the NSLog statement is printing null (sending a message to a nil object in ObjC return nil).
Put this before assigning to activeMissions:
NSMutableDictionary *activeMissions = [NSMutableDictionary dictionaryWithCapacity:3];
Otherwise, if you prefer having a non mutable NSDictionary, you could do:
NSDictionary *activeMissions = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:tiger, bull, peacock, nil]
forKeys: [NSArray arrayWithObjects:#tiger, #"bull", #"peacock", nil]];
(Keep in mind that this is autoreleased, you'll have to retain somehow).

Second object of each pair must be non-nil

Iam working on app and when i debug my code it is showing the following error.
-(IBAction)addSelected:(id)sender
{
NSString* emptyStr = #"";
NSDictionary* traits = [NSDictionary dictionaryWithObjectsAndKeys:
self.walletName.text, #"alias",
self.abc.text, #"abc",
self.field.text, #"field",
#"name," #"Type",
nil];-------at this point it is showing thread recieved signal sigabrt
In the debug cosole it is gving this reason.
'+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did you forget to nil-terminate your parameter list?'
* Call stack at first throw:
#"name," #"Type", should be #"name", #"Type",
The , is inside the string.
NSDictionary* traits = [NSDictionary dictionaryWithObjectsAndKeys:
self.walletName.text, #"alias",
self.abc.text, #"abc",
self.field.text, #"field",
#"name", #"Type", nil];
The problem is form #"name,".
In my case i was doing this:
NSDate *today = [NSDate date];
NSTimeInterval currentTime = [today timeIntervalSince1970];
NSMutableDictionary *dictExpiryDate =
[NSMutableDictionary dictionaryWithObjectsAndKeys:currentTime, KEY_4_VALUE_OF_EXPIRY_DATE, nil];
The problem here is currentTime, i replaced currentTime as followed:
[NSNumber numberWithDouble:currentTime]
This solved my issues.