How to mapping array in YAPdatabase object? - objective-c

i have test array with objects structure - Group with (NSMutableArray)items, and i save group in YapDatabase
-(void)parseAndSaveJson:(id) json withCompleteBlock:(void(^)())completeBlock{
NSMutableArray *groupsArray = (NSMutableArray *)json;
if (groupsArray != nil) {
YapDatabaseConnection *connectnion = [[DatabaseManager sharedYapDatabase] newConnection];
[connectnion asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (int groupIndex = 0; groupIndex < [groupsArray count]; groupIndex ++) {
LocalGroupsExercise *localGroup = [[LocalGroupsExercise alloc] init];
localGroup.name = groupsArray[groupIndex][LOCAL_GROUPS_NAME];
localGroup.tagColor = groupsArray[groupIndex][LOCAL_GROUPS_TAG_COLOR];
localGroup.idGroup = [groupsArray[groupIndex][LOCAL_GROUPS_ID_GROUP] intValue];
if (groupsArray[groupIndex][LOCAL_GROUPS_EXERCISES] != nil) {
NSMutableArray *exerciseArray = (NSMutableArray *)groupsArray[groupIndex][LOCAL_GROUPS_EXERCISES];
for (int exerciseIndex = 0; exerciseIndex < [exerciseArray count]; exerciseIndex ++) {
LocalExercise *localExercise = [[LocalExercise alloc] init];
localExercise.name = exerciseArray[exerciseIndex][EXERCISE_NAME];
localExercise.exerciseId = [exerciseArray[exerciseIndex][LOCAL_EXERCISE_ID_EXERCISE] intValue];
localExercise.groupId = localGroup.idGroup;
localExercise.type = [exerciseArray[exerciseIndex][EXERCISE_TYPE] intValue];
localExercise.minWeight = [exerciseArray[exerciseIndex][EXERCISE_MIN_WEIGHT] floatValue];
localExercise.maxWeight = [exerciseArray[exerciseIndex][EXERCISE_MAX_WEIGHT] floatValue];
localExercise.minReps = [exerciseArray[exerciseIndex][EXERCISE_MIN_REPS] intValue];
localExercise.maxReps = [exerciseArray[exerciseIndex][EXERCISE_MAX_REPS] intValue];
localExercise.minTimer = [exerciseArray[exerciseIndex][EXERCISE_MIN_TIMER] intValue];
localExercise.maxTimer = [exerciseArray[exerciseIndex][EXERCISE_MAX_TIMER] intValue];
localExercise.timeRelax = [exerciseArray[exerciseIndex][EXERCISE_RELAX_TIME] intValue];
[localGroup.exercises addObject:localExercise];
}
}
[transaction setObject:localGroup forKey:[NSString stringWithFormat:#"%d", localGroup.idGroup] inCollection:LOCAL_GROUPS_CLASS_NAME];
}
YapDatabaseConnection *connectnion = [[DatabaseManager sharedYapDatabase] newConnection];
[connectnion readWithBlock:^(YapDatabaseReadTransaction *transaction) {
LocalGroupsExercise *group = [transaction objectForKey:#"2" inCollection:LOCAL_GROUPS_CLASS_NAME];
NSLog(#"%#", group.name);
NSLog(#"%#", group.tagColor);
NSLog(#"%#", group.exercises);
}];
} completionBlock:^{
completeBlock();
}];
}
}
+ (YapDatabaseView *)setupDatabaseViewForShowGroupsGyms{
YapDatabaseViewGrouping *grouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object isKindOfClass:[LocalGroupsExercise class]]) {
LocalGroupsExercise *groupExercise = (LocalGroupsExercise *)object;
return [NSString stringWithFormat:#"%#", groupExercise.name];
}
return nil;
}];
YapDatabaseViewSorting *sorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, LocalGroupsExercise *obj1, NSString *collection2, NSString *key2, LocalGroupsExercise *obj2) {
return [obj1.name compare:obj2.name options:NSNumericSearch range:NSMakeRange(0, obj1.name.length)];
}];
YapDatabaseView *databaseView = [[YapDatabaseView alloc] initWithGrouping:grouping sorting:sorting versionTag:#"0"];
return databaseView;
}
[[DatabaseManager sharedYapDatabase] registerExtension:self.databaseGroupView withName:LOCAL_GROUPS_CLASS_NAME];
[_connection beginLongLivedReadTransaction];
self.mappingsGroup = [[YapDatabaseViewMappings alloc] initWithGroupFilterBlock:^BOOL(NSString *group, YapDatabaseReadTransaction *transaction) {
return true;
} sortBlock:^NSComparisonResult(NSString *group1, NSString *group2, YapDatabaseReadTransaction *transaction) {
return [group1 compare:group2];
} view:LOCAL_GROUPS_CLASS_NAME];
[_connection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[self.mappingsGroup updateWithTransaction:transaction];
}];
The problem is that the group be NSMutabblArray and I want to see the objects in the table of the array, but [self.mappingsGroup numberOfItemsInSection:section] return only one items in group

You need to configure YapDatabase to use Mantle. By default, it will use NSCoding. (Which is why you're seeing an error about "encodeWithCoder:", as that method is part of NSCoding.)
Take a look at YapDatabase's wiki article entitled "Storing Objects", which talks about how it uses the serializer/deserializer blocks: https://github.com/yaptv/YapDatabase/wiki/Storing-Objects
Basically, when you alloc/init your YapDatabase instance, you'll want to pass a serializer & deserializer block that uses Mantle to perform the serialization/deserialization.
Also, see the various init methods that are available for YapDatabase: https://github.com/yaptv/YapDatabase/blob/master/YapDatabase/YapDatabase.h

Related

ObjectForKey is returning nil sometimes

I am trying to set an object to a dictionary with key as an object. The test cases works fine, but within the actual code, I am not able to get the value for the key. The NSMutableDictionary has the key value in it, but when debugging it returns nil.
#implementation JSHashMap {
NSMutableDictionary *dict;
}
- (instancetype)initWithArray:(NSMutableArray *)array {
self = [super init];
if (self) {
dict = [self fromArray:array];
}
return self;
}
- (NSMutableDictionary *)fromArray:(NSMutableArray *)array {
NSMutableDictionary* _dict = [NSMutableDictionary new];
NSUInteger i = 0, len = [array count];
if (len % 2 != 0) {
error(#"JSError: Odd number of elements in the array.");
return _dict;
}
for (i = 0; i < len; i = i + 2) {
[_dict setObject:array[i + 1] forKey:array[i]];
assert([_dict objectForKey:array[i]] != nil);
}
debug(#"%#", _dict);
return _dict;
}
- (JSData *)objectForKey:(id)key {
return [dict objectForKey:key];
}
I am creating the hash map using the initWithArray method.
(lldb) po [dict objectForKey:key]
nil
The key passed in and the key in the dictionary has the same memory address 0x100ea2fa0.
The test cases works fine though. But the when running the actual program, it fails.
NSMutableDictionary *dict = [NSMutableDictionary new];
JSNumber *val = [[JSNumber alloc] initWithInt:1];
JSNumber *key = [[JSNumber alloc] initWithInt:2];
[dict setObject:val forKey:key];
JSData * ret = [dict objectForKey:key];
XCTAssertNotNil(ret);
XCTAssertEqualObjects([ret dataType], #"JSNumber");
JSHashMap *hm = [[JSHashMap alloc] initWithArray:[#[key, val] mutableCopy]];
JSData * ret1 = [hm objectForKey:key];
XCTAssertNotNil(ret1);
XCTAssertEqualObjects([ret1 dataType], #"JSNumber");
JSHashMap *dict = (JSHashMap *)ast;
NSArray *keys = [dict allKeys];
NSUInteger i = 0;
NSUInteger len = [keys count];
for (i = 0; i < len; i++) {
id key = keys[i];
JSData *val = (JSData *)[dict objectForKey:key];
// Issue -> val is getting nil
}
How to fix this and why is this random behaviour?
Found the failing test case.
NSArray *keys = [hm allKeys];
XCTAssertTrue([keys count] == 1);
JSData *ret = [hm objectForKey:keys[0]];
XCTAssertNotNil(ret);
If I use the key returned from calling allKeys method, it returns nil.
You have not shown any information about what JSNumber is, but I am betting that it does not implement isEqual and hash correctly. Thus, you cannot successfully use it as a key in an NSDictionary.

How to obtain Home City from Contacts Framework in iOS +9?

I am replacing my obsolete ABAdressBook code with the current CNContact framework in Objective C. I could sort out most of it, except for the Home City part, so lets focus on that.
Currently I have this code:
-(NSArray *)getLandAddressesForContactIOS6:(ABRecordRef)recordRef {
ABMultiValueRef addresses = ABRecordCopyValue(recordRef, kABPersonAddressProperty);
NSMutableArray *formattedAddressesResponse = [NSMutableArray array];
for(CFIndex i = 0; i < ABMultiValueGetCount(addresses); i++) {
NSString *label = (__bridge NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(addresses, i));
NSDictionary *addressComponents = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(addresses, i);
NSString *street = [addressComponents objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressComponents objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *formattedAddress = ABCreateStringWithAddressDictionary(addressComponents, YES);
NSMutableDictionary *currentAddressResponse = [NSMutableDictionary dictionaryWithObjectsAndKeys:
label, #"type",
label, #"label",
nil];
if (street != nil) {
[currentAddressResponse setObject:street forKey:#"street"];
}
if (city != nil) {
[currentAddressResponse setObject:city forKey:#"city"];
}
if (formattedAddress != nil) {
[currentAddressResponse setObject:formattedAddress forKey:#"formattedAddress"];
}
[formattedAddressesResponse addObject:currentAddressResponse];
}
return formattedAddressesResponse;
}
That code is deprecated for iOS 9+ so the closest I have got to get the home city with the new Contacts framework is:
-(NSArray *)getLandAddressesForContactIOS10:(CNContact*)recordRef {
NSArray <CNLabeledValue<CNPostalAddress *> *> *addresses = recordRef.postalAddresses;
NSMutableArray *formattedAdressResponse = [NSMutableArray array];
for(CFIndex i = 0; i < addresses.count; i++) {
CNLabeledValue *addressi = [addresses objectAtIndex:i];
//NSString *city = addressi.??????; //Stuck here, don't know what else to do
How can I extract the City name from a CNContact??
-(void )getLandAddressesForContactIOS10:(CNContact*)contact
{
for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
{
NSLog(#"%#",labeledValue.value.city);
NSLog(#"%#",labeledValue.value.street);
NSLog(#"%#",labeledValue.value.state);
NSLog(#"%#",labeledValue.value.postalCode);
NSLog(#"%#",labeledValue.value.ISOCountryCode);
}
}
NSArray *addresses = (NSArray*)[contact.postalAddresses valueForKey:#"value"];
if (!(addresses == nil) && addresses.count > 0)
{
for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
{
NSString *city = labeledValue.value.city;
NSLog(#"City = %#",city);
NSString *street = labeledValue.value.street;
NSLog(#"Street = %#",street);
NSString *state = labeledValue.value.state;
NSLog(#"State = %#",state);
NSString *postalCode = labeledValue.value.postalCode;
NSLog(#"PostalCode = %#",postalCode);
NSString *ISOCountryCode = labeledValue.value.ISOCountryCode;
NSLog(#"ISOCountryCode = %#",ISOCountryCode);
}
}
else
{
NSLog(#"No addresses for name = %#",strname);
}

array index out of bounds problems

So I'm making a biginteger program, and I'm having a problem with adding two arrays that aren't the same length. The problem I'm having is with the add method. If I'm iterating through an array is there any way to test if element is out of bounds. I've tried testing if the element in a is equal to nil, but I still get the exception. Any help would be great thanks.
#import <Foundation/Foundation.h>
#import "MPInteger.h"
#implementation MPInteger
{
}
-(id) initWithString: (NSString *) x
{
self = [super init];
if (self) {
intString = [NSMutableArray array];
for (int i = 0; i < [x length]; i++) {
NSString *ch = [x substringWithRange:NSMakeRange(i, 1)];
[intString addObject:ch];
}
}
return self;
}
-(NSString *) description
{
return self.description;
}
- (MPInteger *) add: (MPInteger *) x {
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray array];
NSInteger arrayCount;
if (a < b) {
arrayCount = [b count];
} else {
arrayCount = [a count];
}
int num = 10;
int carry = 1;
NSNumber *total;
NSNumber *carrySum;
for (int i = 0; i < arrayCount; i++) {
if (a[i] == nil) {
total = #([b[i] intValue]);
[c addObject:total];
} else if (b[i] == nil) {
total = #([a[i] intValue]);
[c addObject:total];
} else {
total = #([a[i] intValue] + [b[i] intValue]);
[c addObject:total];
}
}
for (NSInteger j = [c count]-1; j >=0; j--) {
if ([c[j] intValue] >= num) {
total = #([c[j] intValue] - num);
carrySum = #([c[j-1] intValue] + carry);
[c replaceObjectAtIndex:j withObject:total];
[c replaceObjectAtIndex:j-1 withObject: carrySum];
}
}
NSString *str = [c componentsJoinedByString:#""];
NSLog(#"%#", str);
return x;
}
-(MPInteger *) multiply: (MPInteger *) x
{
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray array];
NSMutableArray *sum = [NSMutableArray array];
NSNumber *total;
NSNumber *carrySum;
int num = 10;
NSNumber *endZero = 0;
NSInteger bottomCount = [b count]-1;
while (bottomCount != -1) {
for (int i = 0; i < [a count]; i++) {
total = #([a[i] intValue] * [[b objectAtIndex:bottomCount] intValue]);
if (bottomCount == [b count] -1) {
[c addObject:total];
} else {
[c replaceObjectAtIndex:i withObject:total];
}
}
for (NSInteger j = [c count]-1; j>=0; j--) {
NSString *carry = [NSString stringWithFormat:#"%d", [c[j] intValue]];
NSString *carry2 = [carry substringToIndex:1];
int carryFinal = [carry2 intValue];
NSString *carry3 = [carry2 stringByAppendingString:#"0"];
int carry4 = [carry3 intValue];
if ([c[j] intValue] >= num) {
total = #([c[j] intValue] - carry4);
carrySum = #([c[j-1] intValue] + carryFinal);
[c replaceObjectAtIndex:j withObject:total];
[c replaceObjectAtIndex:j-1 withObject: carrySum];
} else {
if(j == 0) {
if (bottomCount == [b count] -1) {
bottomCount = bottomCount - 1;
NSString *str = [c componentsJoinedByString:#""];
[sum addObject: str];
} else {
[c addObject:#([endZero intValue])];
bottomCount = bottomCount - 1;
NSString *str = [c componentsJoinedByString:#""];
[sum addObject: str];
}
}
}
}
}
NSMutableArray *finalSum = [NSMutableArray array];
MPInteger *ele1;
MPInteger *ele2;
MPInteger *eleSum;
NSNumber *endZ= #(0);
[finalSum insertObject:endZ atIndex:0];
for (int k = 0; k < [sum count]; k++) {
NSString *str= [NSString stringWithFormat:#"%d", [sum[k] intValue]];
NSString *str2 = [NSString stringWithFormat:#"%d", [sum[k+1] intValue]];
ele1 = [[MPInteger alloc] initWithString:str];
ele2 = [[MPInteger alloc] initWithString:str2];
eleSum = [ele1 add: ele2];
NSLog(#"%#", eleSum);
}
NSLog(#"%#", sum);
return self;
}
Updated this
for (int i = 0; i < arrayCount; i++) {
if (a[i] == nil) {
total = #([b[i] intValue]);
[c addObject:total];
} else if (b[i] == nil) {
total = #([a[i] intValue]);
[c addObject:total];
} else {
total = #([a[i] intValue] + [b[i] intValue]);
[c addObject:total];
}
}
has now become:
NSMutableArray *c = a.count > b.count ? [a mutableCopy] : [b mutableCopy];
NSArray *shortestArray = a.count > b.count ? b : a;
[shortestArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSNumber *currentNumber, NSUInteger idx, BOOL *stop) {
c[idx] = #(currentNumber.integerValue + [c[idx] integerValue]);
NSLog(#"%#", c[idx]);
}];
What I think I need to do is every index that is in array a and not b or vise versa, is add beginning zeros, but I don't know how to do that.
I printed out what it does after each iteration and it gives:
2013-09-02 12:31:42.630 Asgn1[42471:303] 5
2013-09-02 12:31:42.632 Asgn1[42471:303] 3
2013-09-02 12:31:42.632 Asgn1[42471:303] 1
And a final answer of:
2013-09-02 12:31:42.633 Asgn1[42471:303] 353
For the code that is failing would it not be simpler to take a mutableCopy of the large array and then loop over the smaller array for the calculations?
Perhaps something like this:
NSMutableArray *c = a.count > b.count ? [a mutableCopy] : [b mutableCopy];
NSArray *shortestArray = a.count > b.count ? b : a;
[shortestArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSNumber *currentNumber, NSUInteger idx, BOOL *stop) {
c[idx] = #(currentNumber.integerValue + [c[idx] integerValue]);
}];

Split NSString with multiple delimiters?

For text bozo__foo!!bar.baz, how to split an NSString containing this into (bozo, foo, bar, baz)?
That is, separe it in components with strings (delimiters) __, !! and ..
You can split the strings using NSCharacterSet. Try this
NSString *test=#"bozo__foo!!bar.baz";
NSString *sep = #"_!.";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *temp=[test componentsSeparatedByCharactersInSet:set];
NSLog(#"temp=%#",temp);
I'm aware that this question has already been answered but this is a way to separate strings using multiple strings. This is a category to NSString.
- (NSArray<NSString *> *)componentsSeparatedByStrings:(NSArray<NSString *> *)separators
{
NSMutableArray<NSString *> *components = [[NSMutableArray alloc] init];
unichar buffer[self.length + 1];
NSInteger currentOrigin = 0;
NSInteger currentLength = 0;
[self getCharacters:buffer];
for(NSInteger i = 0; i < self.length; i++)
{
unichar currentChar = buffer[i];
currentLength++;
for(NSInteger n = 0; n < separators.count; n++)
{
NSString *currentDivider = [separators objectAtIndex:n];
if(currentDivider.length == 0)
{
return #[self];
}
else if(currentDivider.length > 1)
{
BOOL goodMatch = NO;
for(NSInteger x = 0; x < currentDivider.length; x++)
{
unichar charInDivider = [currentDivider characterAtIndex:x];
if(charInDivider == currentChar)
{
goodMatch = YES;
}
else
{
goodMatch = NO;
break;
}
if(goodMatch == YES && ((x + 1) != currentDivider.length))
{
i++;
currentLength++;
currentChar = buffer[i];
}
}
if(goodMatch == YES)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - currentDivider.length));
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = (i + 1);
currentLength = 0;
[components addObject:newComponent];
NSLog(#"%#", components);
}
}
else // If current divider is only one character long.
{
if([currentDivider characterAtIndex:0] == currentChar)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - 1));
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = (i + 1);
currentLength = 0;
[components addObject:newComponent];
break;
}
}
}
// Handle the end of the string.
if((i + 1) == self.length)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, currentLength);
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = 0;
currentLength = 0;
[components addObject:newComponent];
}
}
return components;
}
Example: "ABCD__EFGHI__JKLMNOP-QRST.UV_WXYZ"
NSLog(#"%#", [test componentsSeparatedByStrings:#[#"__", #"-", #"."]]);
Log Result: "(ABCD, EFGHI, JKLMNOP, QRST, "UV_WXYZ")"
NSString *text = #"bozo__foo!!bar.baz";
NSArray *split1 = [text componentsSeparatedByString:#"__"];
NSArray *split2 = [[split1 lastObject] componentsSeparatedByString:#"!!"];
NSArray *split3 = [[split2 lastObject] componentsSeparatedByString:#"."];
NSLog(#"%#, %#, %#, %#", split1[0], split2[0], split3[0], split3[1]);
More functional solution is to apply -componentsSeparatedByString: recursively, for each component, which was derived during previous separator application:
NSString Category
- (NSMutableArray<NSString *> *)gvr_componentsSeparatedByStrings:(NSArray<NSString *> *)separators {
if (separators.count == 0) {
return [NSMutableArray arrayWithObject:self];
}
NSString *separator = [separators firstObject];
NSArray *reducedSeparators = [separators gvr_arrayByRemovingFirstObject];
NSArray *components = [self componentsSeparatedByString:separator];
NSMutableArray *result = [NSMutableArray new];
for (NSString *component in components) {
NSMutableArray *subResult = [component gvr_componentsSeparatedByStrings:reducedSeparators];
[result addObjectsFromArray:subResult];
}
return result;
}
NSArray Category
- (NSArray *)gvr_arrayByRemovingFirstObject {
NSMutableArray *result = [NSMutableArray new];
for (NSInteger i = 1; i < self.count; i++) {
[result addObject:self[i]];
}
return [result copy];
}
I solved it for my project by looking for the longest separator, replacing the others with this one, then do the separation on the only one left.
Try this:
NSString *test = #"bozo__foo!!bar.baz";
test = [test stringByReplacingOccurrencesOfString:#"!!" withString:#"__"];
test = [test stringByReplacingOccurrencesOfString:#"." withString:#"__"];
NSArray<NSString *> *parts = [test componentsSeparatedByString:#"__"];

Get random not repeating value from array

I have method called getRandomCar which is executed via NSTimer for 4 seconds. This method have an array like this
NSArray *cars = [NSArray arrayWithObjects:#"Mercedes", #"Opel", #"Ford", #"Mazda", nil];
The idea is when getRandomCar method is called the returned value to be different from the last returned i.e
Mescedes Opel Mercedes Mazda Ford Opel etc..
not
Mercedes Mercedes Mazda Opel Opel etc...
I have tried this but with no luck.
NSString *car = [[NSString alloc]initWithFormat:#"%#",
[carsArray objectAtIndex:arc4random() % [carsArray count]]];
if ([self.tmpCar isEqualToString:car])
{
car = [carsArray lastObject];
}
[self settmpCar:playerName];
Use this:
NSArray *carsArray = [NSArray arrayWithObjects:#"Mercedes", #"Opel", #"Ford", #"Mazda", nil];
NSMutableString *car = [[NSMutableString alloc] init];
NSMutableString *lastCar = [[NSMutableString alloc] init];
[lastCar setString:#"XXX"];
do {
[car setString:[NSString stringWithFormat:#"%#",
[carsArray objectAtIndex:arc4random() % [carsArray count]]]];
}
while ([lastCar isEqualToString:car]);
[lastCar setString:car];
I'd recommend you to remember the index of the last car. And when you generate a random number check if it's equal to the previous and, if so, generate another number. Sample code:
int test;
int lastValue;
for (int a = 0; a < 10; a++) {
test = arc4random() % 10;
if (test == lastValue) {
while (test == lastValue) {
test = arc4random();
NSLog(#"in While loop");
}
} else {
NSLog(#"%i", test);
}
lastValue = test;
}
Hope it helps
edit
Decided to add the code for you:
NSArray *cars = [NSArray arrayWithObjects:#"Mercedes", #"Opel", #"Ford", #"Mazda", nil];
int test;
int lastValue;
for (int a = 0; a < 20; a++) {
test = arc4random() % [cars count];
if (test == lastValue) {
while (test == lastValue) {
test = arc4random();
}
} else {
NSString *car = [cars objectAtIndex:test];
lastValue = test;
}
}
that will make 20 random non-repeatable cars
You can use another array to hold the cars that have already been picked up before. In the following example, once you exhaust the entire carsArray, method GetCar will return nil. Note that this may not be the most efficient way, but for a small size of carsArray it should work fine.
At the class level:
NSMutableArray *prevCars = [[NSMutableArray alloc] init];
Your method:
-(NSString*) GetCar {
if([prevCars count] == [carsArray count])
return nil;
NSMutableString *car = [[NSMutableString alloc] init];
do {
[car setString:[NSString stringWithFormat:#"%#",
[carsArray objectAtIndex:arc4random() % [carsArray count]]]];
}
while ([InArray:prevCars value:car])
[prevCars addObject:car];
return car;
}
And then:
-(BOOL)InArray:(NSArray *)arr value:(NSString *)val {
for (id Elem in arr) {
if([val isEqualToString:(NSString*)val])
return YES;
}
return NO;
}
Of course, make sure to free the memory when you're done.