change speed of an array loop? Objective-C - objective-c

I have this code
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
for(int i = 0; i<6; i++){
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
and right now they are all printed to the console instantly. How can I make a variable to decrease or increase the speed they are logged?
I'm new at objective-C so thanks a lot for your help! :)

NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
// the number of seconds to wait between printing each item
double secondsToSleep = 1.0;
for(int i = 0; i<6; i++){
[NSThread sleepForTimeInterval:secondsToSleep];
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}

There's a sleepForTimeInterval: method on NSThread that might do what you're looking for. The documentation is here.
Edit: Sorry, for Objective-C newbies, you would just type something like this:
[NSThread sleepForTimeInterval:0.01];

See the sleep() function.

Related

dispatch_apply gives incorrect output data

I have two arrays: array1 and array2. Each object of arrays is an array too (2D arrays). In this way I multiple them. So how I have big arrays I use dispatch_apply. Every time i receive different results include a right result. Maybe somebody knows how to fix it?
dispatch_apply([array2 count], queue, ^(size_t j)
{
k = 0;
for (int l = 0; l < [[array1 objectAtIndex:0] count]; l++) {
k += [[[array1 objectAtIndex:i] objectAtIndex:l] intValue] *
[[[array2 objectAtIndex:j] objectAtIndex:l] intValue];
}
kNSNumber = [NSNumber numberWithInt:k];
[multipliedArrayInto replaceObjectAtIndex:j withObject:kNSNumber];
});
[resulArray insertObject:multipliedArrayInto atIndex:i];
}
There's two things, I can suggest, and I bet one of them (or both) is the overarching solution to your problem.
First, I would declare k local to the block, so there would be no question that you are overwriting it or not. You likely have the same problem with kNSNumber inside the block. If you are just using that NSNumber instance to slam into the multipliedArrayInto accumulator, you may as well remove kNSNumber, and use #(k) in it's place (if only to be more readable). Similarly, make sure multipliedArrayInto is declared just before the dispatch_apply, in what looks like an outer for loop (where ever i is coming from). And finally, make sure resulArray is instantiated, or otherwise readied just before that outer for loop.
Second, is queue a concurrent or serial queue? If you are using dispatch_apply like a parallel-executing for/enumeration -- which is likely, I think, so you are taking about handling "big arrays" efficiently -- then you are practically guaranteeing that k is being overwritten. If you change it to serial, it may work as designed. If you want it to be parallel, you will need to move the declaration of your k accumulator inside the block, and make sure the declaration of other variables makes sense, too.
Update to reflect question updates:
#antonytonies ideally, your followup answer on this thread should be moved into the question itself, so that people can follow this thread easier.
So, it looks like what I described is exactly your problem.
The global queues are all concurrent queues, which means that (hypothetically) all the dispatch blocks are executing at once, and the contents of k and other variables are getting blown away depending on how the order of the blocks executes.
I've taken your update (in the "answer" you added), and modified it to probably work:
// I renamed your method, because nameless parameters pain me. This is cosmetic, and doesn't
// matter for the problem at hand.
- (NSMutableArray *)multiplicationArrays:(NSMutableArray *)array vector:(NSMutableArray *)vector
{
// IMHO, you want to set resultArray to nil here. Another option is to set it to nil in the
// else case, below. Properties in Objective-C are initalized to nil,0,false,etc; you can
// rely on ARC to initialize pointer to objc objects on the stack, too. However, someone
// reading this code may or may not know that. IMHO, using the explicitly assignement makes it
// clear that you're going to be returning `nil` or an instance of `NSMutableArray`.
NSMutableArray *resultArray = nil;
if ([[array objectAtIndex:0] count] == [vector count]) {
// Nicely done w/ pre-allocating the result array here, so that there's no question
// of the indexes matches the results later on.
resultArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
for (int i=0; i < [array count]; i++) {
[resultArray insertObject:[NSNull null] atIndex:i];
}
// 'queue' here is a concurrent queue. This means that you are proclaiming to the runtime
// that the blocks being executed are able to operate correctly w/o interference from each
// other. This is also thought of in terms of parallel execution: all these blocks may run
// **at once**. This *also* means, that you must not share storage between them.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply([array count], queue, ^(size_t j) {
// Moved 'result' inside the block.
NSInteger result = 0;
for (int l = 0; l < [[array objectAtIndex:0] count]; l++) {
// These array reads are **NOT** thread safe. They probably don't cause must trouble in
// practice, but you may want to reconfigure this.
result += [[[array objectAtIndex:j] objectAtIndex:l] intValue] * [[vector objectAtIndex:l] intValue];
}
// The replace of the object into resultArray is **NOT** thread-safe.
// This probably hasn't caused you much trouble, since you can guarantee that
// you aren't writing at the same index. However, I would strongly suggest to
// change this to be thread-safe.
[resultArray replaceObjectAtIndex:j withObject:#(result)];
});
}
else {
NSLog(#"matrix count isn't correspond");
}
return resultArray;
}
Finally: consider just using Apple's Accelerate framework for this sort of problem solving. It's available on OSX and iOS, so you should have all of your bases covered.
it's the same thing if I multiple 2D-array and vector
-(NSMutableArray*)multiplicationArraysWithVector:(NSMutableArray *)array :(NSMutableArray *)vector
{
NSMutableArray* resultArray;
if ([[array objectAtIndex:0] count] == [vector count])
{
resultArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
for (int i=0; i < [array count]; i++) {
[resultArray insertObject:[NSNull null] atIndex:i];
}
__block NSInteger result;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply([array count], queue, ^(size_t j)
{
result = 0;
for (int l = 0; l < [[array objectAtIndex:0] count]; l++) {
result += [[[array objectAtIndex:j] objectAtIndex:l] intValue] * [[vector objectAtIndex:l]intValue];
}
[resultArray replaceObjectAtIndex:j withObject:#(result)];
});
}
else
{
NSLog(#"matrix count isn't correspond");
}
return resultArray;
}
In this case I can get a right or wrong data result.

adding a string followed by an int to an array

I am very new to Objective-C with Cocoa, and I need help.
I have a for statement in which I loop i from 1 to 18, and I would like to add an object to an NSMutableArray in this loop. Right now I have:
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++)
{
[chapterList addObject:#"Chapter"+ i];
}
I would like it to add the objects, chapter 1, chapter 2, chapter 3... , chapter 18. I have no idea how to do this, or even if it is possible. Is there a better way? Please Help
Thanks in advance,
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++)
{
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d",i]];
}
good luck
Try:
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d", i]];
In Objective-C/Cocoa you can't append to a string using the + operator. You either have to use things like stringWithFormat: to build the complete string that you want, or things like stringByAppendingString: to append data to an existing string. The NSString reference might be a useful place to start.
If you're wanting strings that merely say Chapter 1, Chapter 2, you can just do this:
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++) {
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d",i]];
}
And don't forget to release the array when you're done, as you're calling alloc on it.

Populate NSMutableArray with CCSprites via for loop

Hey guys, using obj-c and the cocos2d framework;
How would I populate an NSMutableArray with CCSprite's with a for-loop to reduce code.
I have +100 sprites/images that need to go into this array, so a for-loop is necessary.
This is my current code:
_backgrounds = [[NSMutableArray alloc]initWithCapacity:31];
for (int i = 31; i > 1; i--){
[_backgrounds addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"background_%d.png", i]]];
}
Im looking for something along the lines of this: (Although this code doesnt work),
for (int i = 31; i > 1; i--){
[backgrounds addObject:[CCSprite spriteWithFile:#"background%d.png", i]];
}
Thanks in advanced,
Oliver.
Chances are your shared CCSpriteFrameCache does not contain any values. You must populate it prior to accessing anything from it.

NSThread to slow down Array being printed to console in Objective-C? so confused

So I have an array with objects #"One", "Two", "Three", "Mouse" and I want to have each word of the array NSLog'ed to the console in half second increments. I also would like to be able to switch to 1 second increments instead. Can anyone please help me write this code?
I was tipped that I can use [NSThread sleepForTimeInterval:0.01]; but I dont know how I can do this.
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
// the number of seconds to wait between printing each item
double secondsToSleep = 1.0;
for(int i = 0; i<6; i++){
[NSThread sleepForTimeInterval:secondsToSleep];
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
if thats not the best way, let me know :)

Replacing multiple array contents with a single object?

I have an NSMutableArray with contents I want to replace with NSNull objects.
This is what I do:
NSMutableArray* nulls = [NSMutableArray array];
for (NSInteger i = 0; i < myIndexes.count; i++)
[nulls addObject:[NSNull null]];
[stageMap replaceObjectsAtIndexes:myIndexes withObjects:nulls];
How can I do this more efficiently?
Is there a way to enumerate an NSIndexSet, so I can replace the array content one by one?
Solved
Suggested method turns out to be 2x faster (avg 0.000565s vs 0.001210s):
if (myIndex.count > 0)
{
NSInteger index = [myIndex firstIndex];
for (NSInteger i = 0; i < myIndex.count; i++)
{
[stageMap replaceObjectAtIndex:index withObject:[NSNull null]];
index = [myIndex indexGreaterThanIndex:index];
}
}
You can use a for loop. Start with the first index, use indexGreaterThanIndex: to get the next index, and stop after you hit the last index.
Don't forget to account for an empty index set. Both the first and last index will be NSNotFound in that case. The easiest way is to test the index set's count; if it's zero, don't loop.
Also, what Jason Coco said about profiling. Don't worry too much about efficiency until your program works, and don't go optimizing things until you have run Shark (or Instruments, if that's your thing) and found exactly what is slow.
I realise this is a very old question but I'm posting here in case anyone else finds this question you could use:
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSLog(#"index: %d", idx);
[objectArray replaceObjectAtIndex:idx
withObject:newObject];
}];
Which is a lot more succinct.