By adding this code in viewDidLoad :
NSMutableArray *array = [NSMutableArray arrayWithObjects:
#"Hefeweizen", #"IPA", #"Pilsner", #"Stout", nil];
NSLog(#"array %#",array);
This is the NSLog I get :
array (
Hefeweizen,
IPA,
Pilsner,
Stout
)
How to get values one by one instead of showing total value in NSLog for example:
array{hefeweizen}
array{IPA}
array{Pilsner}
array{Stout}
Ex 1: array[0],array[1]....;
Ex 2: [array objectAtIndex:0]..;
//Initialise More data
setMoreArray = [[NSMutableArray alloc] init];
NSMutableDictionary *setData = [[NSMutableDictionary alloc] init];[setData setObject:#"profile.png" forKey:#"icon"];[setData setObject:#"inloyal Profile" forKey:#"lable"];[setMoreArray addObject:setData];
some thing like this ??
op:
array ->
(
{
"icon" = "profile.png"
},
{
"lable" = "inloyal Profile"
}
)
I tried this code and it worked correctly :
NSUInteger index;
int64_t delayInSeconds = 5.0;
NSMutableArray *array = [NSMutableArray arrayWithObjects:
#"Hefeweizen", #"IPA", #"Pilsner", #"Stout", nil];
NSMutableArray *arrayac = [[NSMutableArray alloc]init];
index = 0;
for ( id item in array )
{
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[arrayac insertObject:item atIndex:index];
NSLog(#"dest array %# ",arrayac);
NSLog(#"last object in the array %# ",[arrayac lastObject]);
testLabel.text = [arrayac lastObject];
});
delayInSeconds += 1;
index++;
}
I placed this code in viewDidLoad and after running the code, exactly after 5sec delay the label changed one after another in a interval of 1sec. I wanted is to show the all values in the array showing up one by one.
Related
I want to store all the emails into an array. What do I need to do for the same ?
Use this code and change it according to your requirement
NSMutableDictionery * repsonseDict = [[NSMutableDictionery alloc]init];
repsonseDict = [reponseObj objectForKey#"response"];
NSArray * allKeys = [repsonseDict allKeys];
NSMutableArray * emailArr = [[NSMutableArray alloc]init];
for(int i = 0; i < [allKeys count]; i ++ ){
NSString * numberKey = [NSString stringWithFormat:#"%d",i];
[emailArr addObject:[[repsonseDict objectForKey:numberKey]objectForKey:#"email"]];
}
I have an output of nsarray like:
2014-12-16 04:55:30.345 Syndasis[17108:7559904] (
"Hello"
)
2014-12-16 04:55:30.348 Syndasis[17108:7559904] (
"Ciao"
)
2014-12-16 04:55:30.351 Syndasis[17108:7559904] (
"Hola"
)
And I don't know how to add to one NSMutableArray. If I make this:
NSMutableArray *ns = [[NSMutableArray alloc] init];
[ns addObject:array];
NSLog(#"%#",ns);
The output is:
2014-12-16 04:59:04.148 Syndasis[17180:7587364] (
(
"Hello"
)
)
2014-12-16 04:59:04.150 Syndasis[17180:7587364] (
(
"Ciao"
)
)
I get the array from:
NSMutableArray *track = [[NSMutableArray alloc] init];
NSDictionary *summary = [dct objectForKey:#"tracks"];
for (NSDictionary *tracks in summary) {
[track addObject:[tracks objectForKey:#"foreign_id"]];
}
NSArray *arrayPR = [track firstObject];
NSMutableArray *ns = [NSMutableArray arrayWithObjects:arrayPR, nil];
NSLog(#"%#",ns);
Thanks!
Track response:
2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
"Hi",
"Hola"
)
2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
"Ciao"
)
I want to make: Make an nsarray from the first string for ever object
2014-12-16 05:25:29.896 Syndasis[17505:7776895] (
"Hi",
"Ciao"
)
I'm not 100% clear about your question, from my understanding, I think you are looking for:
NSMutableArray *ns = [NSMutableArray arrayWithArray: firstArray];
[ns addObjectsFromArray: secondArray];
[ns addObjectsFromArray: thirdArray];
NSMutableArray *track = [[NSMutableArray alloc] init];
NSDictionary *summary = [dct objectForKey:#"tracks"];
for (NSDictionary *tracks in summary)
{
//In tracks dictionary, the object for key 'foreign_id' is a array of strings.
NSArray* lArray = [tracks objectForKey:#"foreign_id"];
//pick first object from array and add it into mutable array.
[track addObject:[lArray firstObject]];
}
NSLog(#"%#",track);
I have one more approach for you. It may be helpful.
Lets add all values in NSMutableString with one separator between each values.
Then separate each values from this string will gives you an object of array.
See below code:
NSMutableString *mString = [NSMutableString new];
for (NSDictionary *tracks in summary) {
[mString appendString:[NSString stringWithFormat:#"%#,",[tracks objectForKey:#"foreign_id"]]];
}
NSMutableArray *array = [NSMutableArray arrayWithArray:[mString componentsSeparatedByString:#","]];
mString = nil;
If albumLists is a mutable array of arrays, and you want an array of the the first objects in each subarray, you can use a KVC Collection Operator:
NSArray* firstTracks = [albumLists valueForKeyPath:#"#unionOfObjects.firstObject"];
Of course, you might have separate arrays album1 & album2 you want to do this for, so you could do this to make the array of arrays and then use the collection operator:
NSArray* firstTracks = [#[album1, album2] valueForKeyPath:#"#unionOfObjects.firstObject"];
FINALLY I GOT THE SOLUTION! I POST THE SOLUTION FOR THE PEOPLE WHO HAVE THE SAME PROBLEM.
I got remembering my past of Javascript developer.
NSMutableArray *nsarray = [NSMutableArray new];
NSArray *array = [[[dct objectForKey:#"tracks"] firstObject] objectForKey:#"foreign_id"];
[nsarray addObject:array];
int len = nsarray.count;
NSMutableArray *var = [[NSMutableArray alloc] init];
for (int i = 0; i < len; i++) {
[var addObject:nsarray[i]];
}
NSLog(#"solution %#",var);
Thanks for the help!
I have array in this format
rows = [[NSArray alloc] initWithObjects:#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica",
#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug" #"drums",
#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral",
#"Mark", #"Madeline",
#"Nemesis", #"nemo", #"name",
#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario",
#"Zeus", #"Zebra", #"zed", nil];
But i need this in to following format
rows = #[#[#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica"],
#[#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug", #"drums"],
#[#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral"],
#[#"Mark", #"Madeline"],
#[#"Nemesis", #"nemo", #"name"],
#[#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario"],
#[#"Zeus", #"Zebra", #"zed"]];
Means that same starting character in to different dictionary
The easiest approach.
NSArray *rows = ...;
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSString *value in rows) {
NSString *firstLetter = [value substringToIndex:1];
if (!map[firstLetter]) {
map[firstLetter] = #[];
}
NSMutableArray *values = [map[firstLetter] mutableCopy];
[values addObject:value];
map[firstLetter] = values;
}
NSArray *finalRows = [map allValues];
Note that finalRows is not sorted.
If you want to sort your array by it's first letter, you can try this :
NSMutableArray *outputArray = [NSMutableArray new];
NSString *lastFirstLetter = nil;
for(NSString *value in rows) {
NSString *firstLetter = [[value substringToIndex:1] lowerString];
if(![lastFirstLetter isEqualToString:firstLetter]) {
lastFirstLetter = firstLetter;
[outputArray addObject:[NSMutableArray new]];
}
[[outputArray lastObject] addObject:value];
}
The idea is to iterate your input array and if the first letter of your word is different than the precedent, create a new array.
I have array of dictionary that needs to be grouped according to PO which is part of the element and also get the total of quantityOrdered according to the same PO.
The PO is dynamic it means it can be any value that needs to be filtered and compute the quantityOrderd accordingly.
Please help.
{
PO = PO2;
QuantityReceived = 1;
},
{
PO = PO1;
QuantityReceived = 3;
},
{
PO = PO1;
QuantityReceived = 3;
},
{
PO = PO3;
QuantityReceived = 2;
},
{
PO = PO2;
QuantityReceived = 2;
},
{
PO = PO3;
QuantityReceived = 4;
},
{
PO = PO1;
QuantityReceived = 1;
},
Apology for the confusion or incomplete question but i need to create a new array of dictionary with similar like this :
{
PO = PO1;
TotalQuanityReceived=7;
LineItems=3;
},
{
PO = PO2;
TotalQuanityReceived=3;
LineItems=2;
},
{
PO = PO3;
TotalQuanityReceived=6;
LineItems=2;
},
i updated my example and make it easy to read.
- (NSArray *)whatever:(NSArray *)dictionaries
{
NSMutableArray *results = [[NSMutableArray alloc] init];
NSMutableDictionary *resultsByPO = [[NSMutableDictionary alloc] init];
for (NSDictionary *dictionary in dictionaries) {
id po = [dictionary objectForKey:#"PO"];
NSMutableDictionary *result = [resultsByPO objectForKey:po];
if (result == nil) {
result = [[NSMutableDictionary alloc] init];
[resultsByPO setObject:result forKey:po];
[results addObject:result];
[result setObject:po forKey:#"PO"];
}
double total = [[result objectForKey:#"TotalQuantityReceived"] doubleValue];
total += [[dictionary objectForKey:#"QuantityOrdered"] doubleValue];
int count = 1 + [[result objectForKey:#"Count"] intValue];
[result setObject:#(total) forKey:#"TotalQuantityReceived"];
[result setObject:#(count) forKey:#"Count"];
}
return results;
}
More pain will come with PO values not conforming to NSCopying.
You can do it the clever way with KVC or the stupid easy way. Let's do it the stupid easy way!
Make an empty NSMutableDictionary. Let's call it dict.
Cycle through your array of dictionaries. For each dictionary:
Fetch its PO. Call that value thisPO.
Fetch dict[thisPO]. Was it nil?
a. Yes. Okay, so this particular PO has not yet been encountered. Set dict[thisPO] to this dictionary's quantity received (as an NSNumber).
b. No. Turn that value into an integer, add this dictionary's quantity received, and set the total back into dict[thisPO] (as an NSNumber).
Done! The result is not quite what you asked for; the result looks like this:
{
PO1 = 100;
PO2 = 120;
...
}
But now, you see, the work of totalling is done and it is easy to transform that into an array of dictionaries if that is what you want.
Not 100% sure if this is what you are saying or not, but to sort an array of dictionaries based on one of the elements it would look something like this.
NSDictionary *d1 = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:100],#"PO",
[NSNumber numberWithDouble:0], #"Category",
nil];
NSDictionary *d2 = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:50],#"PO",
[NSNumber numberWithDouble:90], #"Category",
nil];
NSArray *unsorted = #[d1, d2];
NSArray *sortedArray;
sortedArray = [unsorted sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSDictionary *first = (NSDictionary*)a;
NSDictionary *second = (NSDictionary*)b;
NSNumber *firstPO = [first objectForKey:#"PO"];
NSNumber *secondPO = [second objectForKey:#"PO"];
return [firstPO compare:secondPO];
}];
NSLog(#"unsorted = %#", unsorted);
NSLog(#"sorted = %#", sortedArray);
I wasn't really sure what PO was, so I just used an NSNumber as an example. Look at this page for an overview of how you would compare a customer object. http://nshipster.com/nssortdescriptor/.
Then you can loop through the array, now in the correct order and build your next NSDictionary.
How can I pass
-(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite;
in a
[self performSelector:#selector(//Right here) withObject:nil afterDelay:3];?
You cant put the whole selector inside the #selector(), and withObjectfirst of all only allows one object to be passed over, and neither do I understand how to use it.
How can I pass a method with objects after a delay?
I also tried a workaround where I call
[self performSelector:#selector(waitExplosion) withObject:nil afterDelay:3];
which then runs the action itself, [self explosionFromPoint:c0TileCoord withSprite:bomb];
, but this is a really bad way to do it as I have to re-declare the variables and it's just bad.
How can I pass a method with objects after a delay?
You could use dispatch_after.
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self explosionFromPoint:aPoint withSprite:sprite];
});
Where aPoint and sprite are defined outside of your block.
If you want to pass multiple arguments to a method using performSelector: , keep all the arguments in a NSArray or NSDictionary and then pass that array/dictionary like
[self performSelector:#selector(testWith:) withObject:array afterDelay:3];
Edit
NSArray *array =[NSArray arrayWithObjects:#"arg1",#"arg2"];
[self performSelector:#selector(testWith:) withObject:array afterDelay:3];
-(void)testWith:(NSArray *)array
{
NSString * arg1 =[array objectAtIndex:0];// first argument
NSString *arg2 = [array objectAtIndex:1];// second argument
// do other stuff
}
Another aproach you can use is to pass an NSArray or an NSMutableArray as parameter to your method.
An Example:
NSString *firstItem = #"first Item";
NSNumber *secondItem = [[NSNumber numberWithBool:YES];
YourCustomObject *thirdItem= [[YourCustomObject alloc] init];
//your array to pass
NSArray *arrayToPass = [[NSArray arrayWithObjects:firstItem, secondItem, thirdItem, nil];
//call you method after delay and pass all the objects:
[self oerformSelector:#selector(doStuffWithMultipleParams:) withObject:arrayToPass afterDelay:3.0f];
The method could be used like this:
- (void)doStuffWithMultipleParams:(NSArray *)passedArray{
String *s;
BOOL b;
YourCustomObject *obj;
if ([[passedArray objectAtIndex:0] isKindOfClass:[NSString class]]){
s = [passedArray objectAtIndex:0];
}
if ([[passedArray objectAtIndex:1] isKindOfClass:[NSNumber class]]){
b = [[passedArray objectAtIndex:1] boolValue];
}
if ([[passedArray objectAtIndex:1] isKindOfClass:[YourCustomObject class]]){
obj = [passedArray objectAtIndex:2];
}
if (s || b || obj){
//now do stuff with these objects
}
}
As per the documentation. For your case you can make use of another method of NSObject. ie
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
But unfortunately in this method you can not give the delay.