XML-RPC request Wordpress - ios

I have no problems to get the value of name in terms (name of my blog post category) out of the xmlrpc server response object ...
Server response:
responseArray: (
{ guid = "http://www.domain.com/wp/?p=12";
"post_id" = "123";
terms = (
{ name = "Uncategorized"; }
);
}
)
... with the following lines of Objective-C code:
NSMutableArray *responseArray = [[NSMutableArray alloc] init];
responseArray = [nodeSaveResponse object];
for(i = 0; i < responseArray.count; i++) {
NSString *postLink = [[responseArray objectAtIndex: i] valueForKey: #"guid"];
NSString *postId = [[responseArray objectAtIndex: i] valueForKey: #"post_id"];
NSMutableArray *catArray = [[NSMutableArray alloc] init];
catArray = [[responseArray objectAtIndex: i] valueForKey: #"terms"]];
NSArray *cat = [catArray valueForKey: #"name"];
NSString *myCatString = [cat objectAtIndex: 0];
}
But to send a new blog post fails because the following code to pack the category string is somehow wrong:
NSString *myCatString = #"MyCategory";
NSMutableDictionary *name = [[NSMutableDictionary dictionaryWithObjectsAndKeys: myCatString, #"name", nil];
NSMutableArray *catArray = [[NSMutableArray arrayWithObject: name];
NSMutableDictionary *values = [[NSMutableDictionary alloc] init];
[values setObject: title forKey: #"post_title"];
// and so on with other values - until here everything works well
[values setObject: catArray forKey: #"terms"]; // if this line is called, the request fails
NSArray *params = [NSArray arrayWithObjects: #"1", user, pass, values, nil];
[myRequest setMethod: #"wp.newPost" withParameter: params];
Any idea, where my fault is?
Cheers, Martin

Related

create JSON from 2 arrays in IOS

I'm trying to create dictionary to POST JSON data to server:
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *values = [NSArray arrayWithObjects: orderClass.extraLat, orderClass.extraLon, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
this gives me:
{
lat = (
"54.720746",
"54.719206",
"54.717466"
);
lon = (
"56.011108",
"56.008510",
"56.007031"
);
}
But the aim is to POST data from arrays in format:
[{"lat":"54.720746", "lon":"56.011108" },
{ "lat":"54.719206", "lon":"56.008510"},
{ "lat":"54.717466", "lon":"56.007031"}]
Need your help.
Thanks for paying attention!
As I said in the comment - you need to reverse the steps towards your goal. First dictionaries, then array. And you'll get what you want.
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *lats = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSArray *lons = [NSArray arrayWithObjects:#"4", #"5", #"6", nil];
// your way (not what you want)
NSArray *values = [NSArray arrayWithObjects: lats, lons, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// my recommendation based on what you want
NSMutableArray *postData = [[NSMutableArray alloc] init];
for (int i = 0; i < lats.count; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[lats objectAtIndex:i] forKey:#"lat"];
[dict setObject:[lons objectAtIndex:i] forKey:#"lon"];
[postData addObject:dict];
}
This will get you these results:
(lldb) po postDict
{
lat = (
1,
2,
3
);
lon = (
4,
5,
6
);
}
And
(lldb) po postData
<__NSArrayM 0x7ffb5be4d950>(
{
lat = 1;
lon = 4;
},
{
lat = 2;
lon = 5;
},
{
lat = 3;
lon = 6;
}
)

iOS pass the value instead of reference

When executing the below block. My friends_dict NSMutableDictionary value changes as friend changes. If I'm not wrong this is because the reference of the value in the dictionary is passed along to the friend. How can I pass the value instead of reference and prevent the dictionary from changing.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:#"friend_nearby"]){
NSString *id = [NSString stringWithFormat:#"%#",[object valueForKey:#"id"]];
NSString *spotValue = [NSString stringWithFormat:#"%#",[object valueForKey:#"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue] ) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}
Copy the array and pass in the copy.
NSArray *friendList = [friends_dict valueForKey:selectedFriendId];
NSMutableArray *friendsArray = [friendList mutableCopy];

Sort NSMutableDictionnary from Plist in UICollectionView

I have a uicollectionView which display items from dictionaries (contained in a main dictionary in a plist). I need to sort it with the key "order" which is inside my dictionaries. I can't figure how to do this. I try to compare keys without success using descriptors.
I take the values from nsmutablearrays to display them in the cell.
Here my code (the one thart working unsorted):
//To retreive the data from the plist
NSMutableDictionary *items = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];
//initialize arrays
_order = [[NSMutableArray alloc]initWithCapacity:10];
_name = [[NSMutableArray alloc]initWithCapacity:10];
_role = [[NSMutableArray alloc]initWithCapacity:10];
_image = [[NSMutableArray alloc]initWithCapacity:10];
_description = [[NSMutableArray alloc]initWithCapacity:10];
_email = [[NSMutableArray alloc]initWithCapacity:10];
_googleplus = [[NSMutableArray alloc]initWithCapacity:10];
//load every NSDictionnary on the view
NSMutableDictionary *loadedItem = [[NSMutableDictionary alloc]initWithCapacity:1];
NSMutableDictionary *theMember = [[NSMutableDictionary alloc]initWithCapacity:10];
loadedItem = [items objectForKey:#"Team"];
for (int i = 0; i < loadedItem.count; i++) {
theMember = loadedItem.allValues[i];
NSString *memberOrder = [theMember objectForKey:#"order"];
[_order addObject:memberOrder];
NSLog(#"member order = %#", _order);
NSString *memberName = [theMember objectForKey:#"name"];
[_name addObject:memberName];
NSLog(#"member name = %#", _name);
NSString *memberRole = [theMember objectForKey:#"role"];
[_role addObject:memberRole];
NSLog(#"member role = %#", _role);
NSString *memberImage = [theMember objectForKey:#"image"];
[_image addObject:memberImage];
NSLog(#"member image = %#", _image);
NSString *memberDesc = [theMember objectForKey:#"description"];
[_description addObject:memberDesc];
NSLog(#"member description = %#", _description);
NSString *memberMail = [theMember objectForKey:#"email"];
[_email addObject:memberMail];
NSLog(#"member mail = %#", _email);
NSString *memberGoogleplus = [theMember objectForKey:#"googleplus"];
[_googleplus addObject:memberGoogleplus];
NSLog(#"member googleplus = %#", _googleplus);
}
[self.collectionView reloadData];
}
I dealt with this by changing my mai ndict into an array which is better to manipulate.

WriteToFile Not Writing File in IOS

UPDATE
Whenever I comment out these lines writeToFile will create a file but if I dont,it will not work.
Here is my code..
NSMutableDictionary *allData = [[NSMutableDictionary alloc] init];
NSArray *countries = [self retrieveCountries];
for(NSDictionary *dicCountry in countries){
NSString *countryName = [dicCountry objectForKey:#"en_name"];
NSArray *capital = [self retrieveCapitals:countryName];
NSMutableDictionary *capitalInfo = [[NSMutableDictionary alloc] init];
for(NSDictionary *dictPerCap in capital){
NSString *cap = [dictPerCap objectForKey:#"en_name"];
NSArray *items = [self retrieveItems:cap];
NSArray *categories = [self retrieveCategories:cap];
if(items == nil)
items = [[NSArray alloc] init];
if(categories == nil)
categories = [[NSArray alloc] init];
// store data in a dictionary
NSDictionary* tempCapDic = [NSDictionary dictionaryWithObjectsAndKeys:
items, #"items",
categories, #"categories",
nil];
//store
[capitalInfo setObject:tempCapDic forKey:cap];
}
// store data in a dictionary
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
capital, #"capitals",
capitalInfo, #"info",
nil];
//store
[allData setObject:dataDic forKey:countryName];
}
/**************************/
NSDictionary *rootDict = [NSDictionary dictionaryWithObjectsAndKeys:countries, #"detailedCountries", allData, #"all", nil];
The lines that needs to be commented out inorder to work are:
capital, #"capitals",
capitalInfo, #"info",
in NSDictionary* tempCapDic.
Any idea on how to fix this?

iOS filter an array with an array

I have an array of strings that I want to use as the filter for another array of dictionaries that is created from a plist. For example, if I had a plist of dictionaries that looked like so:
Key: Value:
car1 audi
car2 bmw
car3 bmw
car4 audi
car5 jaguar
and my array of strings was "audi, jaguar". How would I code it so that I can create a new array that would return "car1, car4, car5"? Hope this makes sense. Or better yet, how can I walk down this dictionary and filter it based on a value and then create a new array of dictionaries to use.
Code:
-(void)plotStationAnnotations {
desiredDepartments = [[NSMutableArray alloc] init];
BOOL tvfrSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"tvfrSwitchStatus"];
BOOL hfdSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"hfdSwitchStatus"];
if (tvfrSwitchStatus) {
NSString *tvfr = #"TVF&R";
[desiredDepartments addObject:tvfr];
}
if (hfdSwitchStatus) {
NSString *hfd = #"HFD";
[desiredDepartments addObject:hfd];
}
NSLog(#"Array 1 = %#", desiredDepartments);
NSString *path = [[NSBundle mainBundle] pathForResource:#"stationAnnotations" ofType:#"plist"];
NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSMutableArray *newDictionaryArray = [NSMutableArray array];
for (NSDictionary *dictionary in anns) {
for (NSString *string in desiredDepartments) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}
NSLog(#"Array = %#", keyMutableArray);
for (int i = 0; i < [keyMutableArray count]; i++) {
float realLatitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"latitude"] floatValue];
float realLongitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"longitude"] floatValue];
StationAnnotations *myAnnotation = [[StationAnnotations alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[keyMutableArray objectAtIndex:i] objectForKey:#"station"];
myAnnotation.subtitle = [[keyMutableArray objectAtIndex:i] objectForKey:#"department"];
[mapView addAnnotation:myAnnotation];
}
}
May be something like
NSArray *array1;
if([array1 containsObject : someValue])
can help. someValue can be your values you want to check if they exist in array1.
You can do something like this to filter by keys:
NSArray *keysToLookFor = [NSArray arrayWithObjects:#"car1", #"car4", #"car5", nil];
NSArray *foundObjects = [dictionary objectsForKeys:keysToLookFor notFoundMarker:nil];
Or something like this to filter by values:
NSString *valueToLookFor = #"Audi";
NSArray *keyArray = [dictionary allKeysForObject:valueToLookFor];
// To filter by multiple values
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
NSMutableArray *keyMutableArray = [NSMutableArray array];
for (NSString *string in valuesToFilterBy) {
[keyMutableArray addObjectsFromArray:[dictionary allKeysForObject:string]];
}
Updated answer for dictionaries in arrays:
NSArray *dictionaryArray; // The array of dictionaries that you have
NSMutableArray *newDictionaryArray = [NSMutableArray array];
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
for (NSDictionary *dictionary in dictionaryArray) {
for (NSString *string in valuesToFilterBy) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}

Resources