Random Number Generator Objective-C iOS8 [duplicate] - objective-c

This question already has answers here:
Picking a Random Object in an NSArray
(8 answers)
Closed 8 years ago.
My app has a mutable array in a data model (used to pass data between views) which allows users to add the names of 'housemates' as strings to a table view controller. I want to later generate a random housemate in another view controller i.e. when a button is pressed I want the label underneath it to display the randomly selected housemate. I wondered if I could assign each housemate a number and, when a random number is generated, use an if/else statement to say something like:
if randomNumber == 0 {
self.label.text = self.housemate0.text;
}
But I'm unsure how to count all the housemates in the mutable array and then assign/tag them with a number to later be generated and how the actual code for generating the number/updating the label would work. Could anyone help me please? Many thanks.

A NSArray, or NSMutableArray is index based, so every entry has an unique index number to access it like myarray.objectAtIndex:1
And counting your array will be done by myarray.count
Is that what you expected with your question?!? :-)

Related

Sort other arrays based on one array (Obj-C) [duplicate]

This question already has answers here:
Sorting two NSArrays together side by side
(4 answers)
Order two NSMutableArrays based on one
(2 answers)
Closed 5 years ago.
I have three NSMutableArray's:
NSMutableArray *dateArray;
NSMutableArray *nameArray;
NSMutableArray *imageArray
I would like to sort the dateArray based on the closest date, and then have the nameArray and imageArray bases on this new sorted order of the dateArray.
How can I accomplish this without changing the structure that I've already setup above?
Without considering why you have three arrays here is an algorithm to "sort" them together:
Create a new array the same size as your three arrays filled with integers starting from 0[†].
"Sort" this new array using sortedArrayUsingComparator: passing a block to the comparison.
Your block will get called with two values from the array, being two integers. Use those integers to index into dataArray and compare the values you find there, returning the result as the block result.
After this you will have an array of integers you can use to indirectly index into your three arrays, e.g. something like:
NSArray *sortedIndicies = [arrayOfInts sortedArrayUsingComparator:...];
// first item according to the sort
id firstDate = dateArray[sortedIndicies[0]];
id firstName = nameArray[sortedIndicies[0]];
You can of course use sortedIndicies to reorder your original 3 arrays if required.
There are other ways to achieve what you require, for example you can "zip" your three arrays into a single array of three items, sort, then "unzip" again. Zip operations are not provided by NSArray so you need to write them (both are a simple loop).
HTH
[†] You can use a "fake" array for this is you wish, for inspiration see this answer - your implementation could be simpler.

How do I query an NSArray when I know the attribute name and value? [duplicate]

This question already has answers here:
How to quickly search an array of objects in Objective-C
(4 answers)
NSPredicate for property of object in NSArray of NSArray
(1 answer)
Iterate through custom objects in a UIView and find matching properties
(2 answers)
Closed 5 years ago.
I am having a hard time getting the correct query type in my NSArray search, I tried the following which failed because its not searching for the correct value
BOOL isCashTender = [_tenderRows containsObject: #"Cash"];
I want to search inside the NSArray where there is a type attribute that contains the value Cash
I don't care how many rows/objects are inside the array I simply need to know if there is at least one type attribute containing the value Cash in there

Accessing variables with related names in a for loop [duplicate]

This question already has an answer here:
Syntax help - Variable as object name [duplicate]
(1 answer)
Closed 8 years ago.
I have three NSRects in separate variables named rect1, rect2, and rect3, and I want to increment each one's origin.x by 10.
I thought a for-loop would be good for this, but I don't know how I can do it.
This is an example of how I want it to work:
for(int i=0, i<3, i++) {
rect[i].origin.x +=10
}
but this exact code gives an error
property "rect" not found on object of type "graphics"
Is there a way to code it like in my example?
Is there a way to increment a variable with for loop
Not in objective-c for sure. You cannot declare variables in 0-n format and loop through it.
If you create an array out of your NSRect variables then you can loop through the array and modify properties.
Note: NSArray can only hold Objects not primitives, you might find this handy

Generate random index which is not generated again [duplicate]

This question already has answers here:
Non repeating random numbers in Objective-C
(6 answers)
Closed 9 years ago.
I have an array of names and an array of numbers.
My Person class has a property for both the name and the number. I want to assign the random values to random people.
Suppose I have an array with the strings #"mary" , #"Jack" and #"ABhraham"
and another array with the numbers 122, 378, 987, I want to assign them these values randomly. I used the arc4random() method but it sometimes returns the same value again.
Use Fisher–Yates shuffle. Create an array that has numbers zero through N-1, inclusive, where N is the number of elements in your arrays; this will be the array of indexes. Then apply Fisher-Yates shuffle to the array of indexes. Now you can use number[index[i]] for each name[i].
Here is a link to an answer that provides a Fisher-Yates Shuffle implementation in Objective-C.
1: Use dictionaries, set keys for each person.
OR
2: Use mutable arrays for both, removing each object as you assign it.
while(arrayOfNumberTags.count>0){
float randomIndex = arc4random() % arrayOfNumberTags.count;
int tagForPerson = [arrayOfNumberTags objectAtIndex:randomIndex];
//Create the Person object with the tag and any person
[arrayOfNumberTags removeObjectAtIndex:randomIndex];
}

Getting a list of all object properties [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get an object attributes list in Objective-C
I am trying to find a way to iterate through all properties on an object and assign the first non empty property's value to another variable. All these properties are string and i am trying to use isEqualToString:#"" to check each property manually before assigning the first non empty one to another variable. Is there a way to do this efficiently by iterating through each property instead of having if conditionals checking each property individually?
Giuseppe's answer is good but it seems you don't need to complicate yourself with that.
If you say all propertys are strings, why not use an NSArray and ,using fast enumeration, check if the string is in the array , if not just add it. That way you don't have a limit (in case you need to modify the project later).
Also this could work even with different types of variables/propertys. When you loop through the array just use isSubclassOfClass: to check type.