How can I compare installed iOS version with the latest iOS version programatically using objective-c?
You have to take new version number from web service.This is because if you want to update app from app store then you just need to increase your app version from your pannel and you will get it in web service and then you can check it like this (call this method by passing web service version) :-
(BOOL)isUpdateAvailable:(NSString*)latestVersion
{
NSString *currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleShortVersionString"];
NSArray *myCurrentVersion = [currentAppVersion componentsSeparatedByString:#"."];
NSArray *myLatestVersion = [latestVersion componentsSeparatedByString:#"."];
NSInteger legthOfLatestVersion = myLatestVersion.count;
NSInteger legthOfCurrentVersion = myCurrentVersion.count;
if (legthOfLatestVersion == legthOfCurrentVersion)
{
for (int i=0; i<myLatestVersion.count; i++)
{
if ([myCurrentVersion[i] integerValue] < [myLatestVersion[i] integerValue])
{
return true;
}
else if ([myCurrentVersion[i] integerValue] == [myLatestVersion[i] integerValue])
{
continue;
}
else
{
return false;
}
}
return false;
}
else
{
NSInteger count = legthOfCurrentVersion > legthOfLatestVersion ? legthOfLatestVersion : legthOfCurrentVersion;
for (int i=0; i<count; i++)
{
if ([myCurrentVersion[i] integerValue] < [myLatestVersion[i] integerValue])
{
return true;
}
else if([myCurrentVersion[i] integerValue] > [myLatestVersion[i] integerValue])
{
return false;
}
else if ([myCurrentVersion[i] integerValue] == [myLatestVersion[i] integerValue])
{
continue;
}
}
if (legthOfCurrentVersion < legthOfLatestVersion)
{
for (NSInteger i=legthOfCurrentVersion; i<legthOfLatestVersion; i++)
{
if ([myLatestVersion[i] integerValue] != 0)
{
return YES;
}
}
return NO;
}
else
{
return NO;
}
}
}
This will return bool value if it will return YES then you will have new version of your app and if NO then your app has updated version.
Related
I have seen many of the answers but didn't get my answer. So that's why I decide to post a question. If anybody can find the helpful link or answer will be helpful.
Here is my array of dictionary:
<__NSArrayM 0x283ba04e0>(
{
failvalues = (
"Check 1"
);
fieldname = "Check 3";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 0;
},
{
failvalues = (
Fail
);
fieldname = "Dropdown 3";
fieldvalue = (
Fail
);
showtype = mandatory;
tagvalue = 1;
},
{
failvalues = (
"Check 1",
"Check 4"
);
fieldname = "Check 4";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 2;
})
So I want to check if the fieldvalue contains failvalues or not.
Below is the code which I have tried but it doesn't seem to work:
for (int i = 0; i< [arrFields count]; i++) {
if ([[[arrFields objectAtIndex:i]objectForKey:#"failvalues"] isKindOfClass:[NSArray class]]) {
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
for (int j = 0; j < [failValues count]; j++) {
if ([failValues containsObject:[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]]) {
NSLog(#"Contains %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
} else {
NSLog(#"No fail values");
}
}
} else {
NSLog(#"No Fail Fields");
}
} else {
NSLog(#"Not an array");
}
}
EDIT:
This one I have tried but how to break both the loops
for (int i = 0; i< [arrFields count]; i++) {
NSArray *fieldValues = [[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"];
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
//NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
for(NSString *value in fieldValues){
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
scanStatus = TRUE;
return;
}
}
} else {
NSLog(#"No Fail Fields");
}
}
Thanks in advance!
This is a classic use case for the feared and dreaded goto. It's well known that goto can create disastrously messy code, but in cases like this it'll make things cleaner. You have to know when to use goto, and definitely use it sparingly. But here's how I'd write your code:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
goto leaveLoops;
}
}
}
}
leaveLoops:
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
And if you cannot bring yourself to use goto (you wouldn't be alone,) here's a gotoless alternative:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
break;
}
}
if (found) break;
}
}
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
I'm plotting Real Time ECG using CorePlot library.When it uses in iPad air the performance is okay.But when i tried it with iPad mini, There is a delay in the plotting.I have done with collapseLayer and this link also.that didn't solved my problem.Can anyone suggest new solution for this.
My code is below:
-(void)newData:(NSTimer *)theTimer
{
for (int i =0;i<plotcount;i++){
if([Qrrch0 count]>0 || [Qrrch1 count]>0 || [Qrrch2 count]>0 || [Qrrch3 count]>0 || [Qrrspo2 count]>0 ){
if(g1==1 && thePlot){
currentIndex ++;
}
if(g2==1 && thePlot1){
currentIndex1 ++;
}
if(g3==1 && thePlot2){
currentIndex2 ++;
}
if(g4==1 && thePlot3){
currentIndex3 ++;
}
if(spo2==1 && thePlot4){
currentIndex4 ++;
}
if(arrayIndex>=kchannel1-1)
{
arrayIndex=0;
}
if(arrayIndex1>=kchannel2-1)
{
arrayIndex1=0;
}
if(arrayIndex>=kchannel1-1)
{
arrayIndex=0;
}
if(arrayIndex2>=kchannel3-1)
{
arrayIndex2=0;
}
if(arrayIndex3>=kchannel4-1)
{
arrayIndex3=0;
}
if(arrayIndex4>=kchannel5-1)
{
arrayIndex4=0;
}
if(g1==1 && thePlot){
currentIndex5++;
if(currentIndex5>=kchannel1)
{
if(arrayIndex==0)
{
[thePlot reloadDataInIndexRange:NSMakeRange(arrayIndex, arrayIndex)];
}else{
[thePlot deleteDataInIndexRange:NSMakeRange(arrayIndex, 1)];
}
}
if([Qrrch0 count]!=0)
{
arrPlot[arrayIndex]=[[Qrrch0 objectAtIndex:0] integerValue];
lastPlot0=[Qrrch0 objectAtIndex:0];
}else{
arrPlot[arrayIndex]=[lastPlot0 integerValue];
}
arrayIndex++;
}
if(g2==1 && thePlot1){
currentIndex6++;
if(currentIndex6>=kchannel2)
{
if(arrayIndex1==0)
{
[thePlot1 reloadDataInIndexRange:NSMakeRange(arrayIndex1, arrayIndex1)];
}else{
[thePlot1 deleteDataInIndexRange:NSMakeRange(arrayIndex1, 1)];
}
}
if([Qrrch1 count]!=0)
{
arrPlot1[arrayIndex1]=[[Qrrch1 objectAtIndex:0] integerValue];
lastPlot1=[Qrrch1 objectAtIndex:0];
}else{
arrPlot1[arrayIndex1]=[lastPlot1 integerValue];
}
arrayIndex1++;
}
if(g3==1 && thePlot2){
currentIndex7++;
if(currentIndex7>=kchannel3)
{
if(arrayIndex2==0)
{
[thePlot2 reloadDataInIndexRange:NSMakeRange(arrayIndex2, arrayIndex2)];
}else{
[thePlot2 deleteDataInIndexRange:NSMakeRange(arrayIndex2, 1)];
}
}
if([Qrrch2 count]!=0)
{
arrPlot2[arrayIndex2]=[[Qrrch2 objectAtIndex:0] integerValue];
lastPlot2=[Qrrch2 objectAtIndex:0];
}else{
arrPlot2[arrayIndex2]=[lastPlot2 integerValue];
}
arrayIndex2++;
}
if(g4==1 && thePlot3){
currentIndex8++;
if(currentIndex8>=kchannel4)
{
if(arrayIndex3==0)
{
[thePlot3 reloadDataInIndexRange:NSMakeRange(arrayIndex3, arrayIndex3)];
}else{
[thePlot3 deleteDataInIndexRange:NSMakeRange(arrayIndex3, 1)];
}
}
if([Qrrch3 count]!=0)
{
arrPlot3[arrayIndex3]=[[Qrrch3 objectAtIndex:0] integerValue];
lastPlot3=[Qrrch3 objectAtIndex:0];
}else{
arrPlot3[arrayIndex3]=[lastPlot3 integerValue];
}
arrayIndex3++;
}
if(spo2==1 && thePlot4){
currentIndex9++;
if(currentIndex9>=kchannel5)
{
if(arrayIndex4==0)
{
[thePlot4 reloadDataInIndexRange:NSMakeRange(arrayIndex4, arrayIndex4)];
}else{
[thePlot4 deleteDataInIndexRange:NSMakeRange(arrayIndex4, 1)];
}
}
if([Qrrspo2 count]!=0)
{
arrPlot4[arrayIndex4]=[[Qrrspo2 objectAtIndex:0] integerValue];
lastPlot4=[Qrrspo2 objectAtIndex:0];
}else{
arrPlot4[arrayIndex4]=[lastPlot4 integerValue];
}
arrayIndex4++;
}
if(g1==1 && thePlot){
if([Qrrch0 count]!=0)
{
[Qrrch0 removeObjectAtIndex:0];
}
if(currentIndex>=kchannel1)
{
currentIndex=1;
}
[thePlot insertDataAtIndex:currentIndex-1 numberOfRecords:1];
}
if(g2==1 && thePlot1){
if([Qrrch1 count]!=0)
{
[Qrrch1 removeObjectAtIndex:0];
}
if(currentIndex1>=kchannel2)
{
currentIndex1=1;
}
[thePlot1 insertDataAtIndex:currentIndex1-1 numberOfRecords:1];
}
if(g3==1 && thePlot2){
if([Qrrch2 count]!=0)
{
[Qrrch2 removeObjectAtIndex:0];
}
if(currentIndex2>=kchannel3)
{
currentIndex2=1;
}
[thePlot2 insertDataAtIndex:currentIndex2-1 numberOfRecords:1];
}
if(g4==1 && thePlot3){
if([Qrrch3 count]!=0)
{
[Qrrch3 removeObjectAtIndex:0];
}
if(currentIndex3>=kchannel4)
{
currentIndex3=1;
}
[thePlot3 insertDataAtIndex:currentIndex3-1 numberOfRecords:1];
}
if(spo2==1 && thePlot4){
if([Qrrspo2 count]!=0)
{
[Qrrspo2 removeObjectAtIndex:0];
}
if(currentIndex4>=kchannel5)
{
currentIndex4=1;
}
[thePlot4 insertDataAtIndex:currentIndex4-1 numberOfRecords:1];
}
}
else
{
[self datacha];
}
}
}
-(void) datacha{
NSArray *array;
if([FinalArray count]>0){
if(g1==1 && thePlot){
array = [[NSArray alloc] initWithArray:[FinalArray objectAtIndex:0]];
[Qrrch0 addObjectsFromArray:array];
}
if(g1==1 && thePlot){
[FinalArray removeObjectAtIndex:0];
}
}
if([FinalArray1 count]>0){
if(g2==1 && thePlot1){
array = [[NSArray alloc] initWithArray:[FinalArray1 objectAtIndex:0]];
[Qrrch1 addObjectsFromArray:array];
}
if(g2==1 && thePlot1){
[FinalArray1 removeObjectAtIndex:0];
}
}
if([FinalArray2 count]>0){
if(g3==1 && thePlot2){
array = [[NSArray alloc] initWithArray:[FinalArray2 objectAtIndex:0]];
[Qrrch2 addObjectsFromArray:array];
}
if(g3==1 && thePlot2){
[FinalArray2 removeObjectAtIndex:0];
}
} if([FinalArray3 count]>0){
if(g4==1 && thePlot3){
array = [[NSArray alloc] initWithArray:[FinalArray3 objectAtIndex:0]];
[Qrrch3 addObjectsFromArray:array];
}
if(g4==1 && thePlot3){
[FinalArray3 removeObjectAtIndex:0];
}
}
if([FinalArray4 count]>0){
if(spo2==1 && thePlot4){
array = [[NSArray alloc] initWithArray:[FinalArray4 objectAtIndex:0]];
[Qrrspo2 addObjectsFromArray:array];
}
if(spo2==1 && thePlot4){
[FinalArray4 removeObjectAtIndex:0];
}
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if(plot == thePlot)
{
if(currentIndex5>=kchannel1)
{
return kchannel1;
}else{
return currentIndex5;
}
}
if(plot == thePlot1)
{
if(currentIndex6>=kchannel2)
{
return kchannel2;
}else{
return currentIndex6;
}
}
if(plot == thePlot2)
{
if(currentIndex7>=kchannel3)
{
return kchannel3;
}else{
return currentIndex7;
}
}
if(plot == thePlot3)
{
if(currentIndex8>=kchannel4)
{
return kchannel4;
}else{
return currentIndex8;
}
}
if(plot == thePlot4)
{
if(currentIndex9>=kchannel5)
{
return kchannel5;
}else{
return currentIndex9;
}
}
return 0;
}
-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx{
double num;
NSString *textval = [NSString stringWithFormat:#"%d", countofpktloss];
self.losscount.text = textval;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
if(plot == thePlot){
num =currentIndex;
}
else if(plot == thePlot1)
{
num = currentIndex1;
}
else if(plot == thePlot2)
{
num = currentIndex2;
}
else if(plot == thePlot3)
{
num = currentIndex3;
}
else if(plot == thePlot4)
{
num = currentIndex4;
}
break;
case CPTScatterPlotFieldY:
if(plot == thePlot){
num =arrPlot[idx];
}
else
if(plot == thePlot1){
num =arrPlot1[idx];
}
else if(plot == thePlot2){
num =arrPlot2[idx];
}
else if(plot == thePlot3)
{
num =arrPlot3[idx];
}
else if(plot == thePlot4)
{
num =arrPlot4[idx];
}
break;
default:
break;
}
return num;
}
The collapsesLayers property is there to help save memory for a static graph. Using it on a graph that updates frequently makes the performance worse since it requires the graph to redraw everything, not just the part that changed, e.g., the plot.
Added comments
Make sure the timer stops when you expect it to. Otherwise, it will keep adding points to the plots.
How often does the timer fire to add points to the plots? There's no point in updating the plots more than 60 times per second and you may need to reduce that further on older devices like the iPad 2 to get good performance with lots of data points. You can add more than one point in each update if needed.
Check the ranges used to reload data points. You're passing the same number as the location and length of the range. I suspect you mean to use a length of one (1) for each range. If so, you're reloading more data than required on each pass.
Have you tried looking at other chart libraries? SciChart provides an iOS chart tailored for realtime ECG applications. It's extremely fast and doesn't have the performance problems of Core plot.
See iOS Chart Performance Comparison
and SciChart iOS ECG demo
Disclosure, I am the tech lead on the scichart projects
Here's the source code for the method that appears to be causing the leak.
- (void)search:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
NSArray* fields = [command argumentAtIndex:0];
NSDictionary* findOptions = [command argumentAtIndex:1 withDefault:[NSNull null]];
[self.commandDelegate runInBackground:^{
// from Apple: Important You must ensure that an instance of ABAddressBookRef is used by only one thread.
// which is why address book is created within the dispatch queue.
// more details here: http: //blog.byadrian.net/2012/05/05/ios-addressbook-framework-and-gcd/
CDVAddressBookHelper* abHelper = [[CDVAddressBookHelper alloc] init];
CDVContacts* __weak weakSelf = self; // play it safe to avoid retain cycles
// it gets uglier, block within block.....
[abHelper createAddressBook: ^(ABAddressBookRef addrBook, CDVAddressBookAccessError* errCode) {
if (addrBook == NULL) {
// permission was denied or other error - return error
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageToErrorObject:errCode ? (int)errCode.errorCode:UNKNOWN_ERROR];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
return;
}
NSArray* foundRecords = nil;
// get the findOptions values
BOOL multiple = NO; // default is false
NSString* filter = nil;
NSArray* desiredFields = nil;
if (![findOptions isKindOfClass:[NSNull class]]) {
id value = nil;
filter = (NSString*)[findOptions objectForKey:#"filter"];
value = [findOptions objectForKey:#"multiple"];
if ([value isKindOfClass:[NSNumber class]]) {
// multiple is a boolean that will come through as an NSNumber
multiple = [(NSNumber*)value boolValue];
// NSLog(#"multiple is: %d", multiple);
}
desiredFields = [findOptions objectForKey:#"desiredFields"];
// return all fields if desired fields are not explicitly defined
if (desiredFields == nil || desiredFields.count == 0) {
desiredFields = [NSArray arrayWithObjects:#"*", nil];
}
}
NSDictionary* searchFields = [[CDVContact class] calcReturnFields:fields];
NSDictionary* returnFields = [[CDVContact class] calcReturnFields:desiredFields];
NSMutableArray* matches = nil;
if (!filter || [filter isEqualToString:#""]) {
// get all records
foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);
if (foundRecords && ([foundRecords count] > 0)) {
// create Contacts and put into matches array
// doesn't make sense to ask for all records when multiple == NO but better check
int xferCount = multiple == YES ? (int)[foundRecords count] : 1;
matches = [NSMutableArray arrayWithCapacity:xferCount];
for (int k = 0; k < xferCount; k++) {
CDVContact* xferContact = [[CDVContact alloc] initFromABRecord:(__bridge ABRecordRef)[foundRecords objectAtIndex:k]];
[matches addObject:xferContact];
xferContact = nil;
}
}
} else {
foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);
matches = [NSMutableArray arrayWithCapacity:1];
BOOL bFound = NO;
int testCount = (int)[foundRecords count];
for (int j = 0; j < testCount; j++) {
CDVContact* testContact = [[CDVContact alloc] initFromABRecord:(__bridge ABRecordRef)[foundRecords objectAtIndex:j]];
if (testContact) {
bFound = [testContact foundValue:filter inFields:searchFields];
if (bFound) {
[matches addObject:testContact];
}
testContact = nil;
}
}
}
NSMutableArray* returnContacts = [NSMutableArray arrayWithCapacity:1];
if ((matches != nil) && ([matches count] > 0)) {
// convert to JS Contacts format and return in callback
// - returnFields determines what properties to return
#autoreleasepool {
int count = multiple == YES ? (int)[matches count] : 1;
for (int i = 0; i < count; i++) {
CDVContact* newContact = [matches objectAtIndex:i];
NSDictionary* aContact = [newContact toDictionary:returnFields];
[returnContacts addObject:aContact];
}
}
}
// return found contacts (array is empty if no contacts found)
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:returnContacts];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
// NSLog(#"findCallback string: %#", jsString);
if (addrBook) {
CFRelease(addrBook);
}
}];
}]; // end of workQueue block
return;
}
The specific line that is doing most of the leaking is foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);, but this is confusing, given that the correct __bridge_transfer call is used. What's going on here?
I've been trying to sort an NSArray of NSDictionaries using a comparator, but I cannot seem to get the output I desire.
The output I'm trying to achieve is that A-Z usernames should come first in the sorted array, then usernames that start with a digit should come second in the sorted array, and lastly usernames that start with an underscore should be last in the sorted array. Any help is truly appreciated!
EDIT: It should be sorted so it looks consistent through the whole NSArray so that: _Anna comes before _Bob and _11Bob comes before _12Cary but after _09Bob
Example of desired output I'm looking for:
(
{
username = abcd;
},
{
username = Anna;
},
{
username = 01Bob;
},
{
username = 02Tob;
},
{
username = 03ZED;
},
{
username = 04_Hob;
},
{
username = 04_sob;
},
{
username = "_anna";
},
{
username = "_bob";
},
{
username = "_boc";
},
{
username = "_bocd12";
},
{
username = "_bocd13";
}
{
username = _01Bob;
},
{
username = _02Tob;
},
)
I hope that makes sense now.
Sample NSDictionary with an NSArray of NSDictionaries:
NSDictionary *dictionary = #{#"users":#[#{#"username":#"191anna"},#{#"username":#"_091bob"},#{#"username":#"Bob"},#{#"username":#"charlie"}]};
I'm trying by using this comparator:
NSArray *array = [[dictionary objectForKey:#"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *f1 = [obj1 objectForKey:#"username"];
NSString *f2 = [obj2 objectForKey:#"username"];
NSString *s1 = [[obj1 objectForKey:#"username"]substringFromIndex:1];
NSString *s2 = [[obj2 objectForKey:#"username"]substringFromIndex:1];
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
{
return [f1 localizedCaseInsensitiveCompare:f2];
}
else if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
{
return [f1 localizedCaseInsensitiveCompare:f2];
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound)
{
return NSOrderedDescending;
}
}
return NSOrderedAscending;
}];
But it gives me the following (not the way I want) sorted NSArray:
(
{
username = "_091bob";
},
{
username = 191anna;
},
{
username = Bob;
},
{
username = charlie;
}
)
Here's what I came up with. It's a touch long because it requires quite a bit of logic. It can likely be optimized further:
My Set Up:
NSArray * usernames = #[#"191anna", #"abcd", #"Anna", #"01Bob", #"02Tob", #"03ZED", #"04_rob", #"_anna", #"_bob", #"_boc", #"_bocd12", #"_bocd13", #"_01Bob", #"_02Tob"];
NSMutableArray * users = [NSMutableArray array];
for (NSString * username in usernames) {
[users addObject:#{#"username":username}];
}
NSDictionary * dictionary = #{#"users":users};
And The Sort:
NSArray *sortedArray = [dictionary[#"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *nameOne = obj1[#"username"];
NSString *nameTwo = obj2[#"username"];
NSString *startOne;
NSString *startTwo;
NSInteger currentIndex = 0;
NSInteger maxIndex = (nameOne.length < nameTwo.length) ? nameOne.length : nameTwo.length;
// Get our first differentiating letter
do {
if (currentIndex < maxIndex) {
startOne = [nameOne substringWithRange:NSMakeRange(currentIndex, 1)];
startTwo = [nameTwo substringWithRange:NSMakeRange(currentIndex, 1)];
currentIndex++;
}
else {
// Names are equal up to max length. Same length is same, else shorter word ascending. (bob above bobb)
if (nameOne.length == nameTwo.length) {
return NSOrderedSame;
}
else {
return (nameOne.length < nameTwo.length) ? NSOrderedAscending : NSOrderedDescending;
}
}
} while ([startOne isEqualToString:startTwo]);
// Prioritize underscores to bottom
NSCharacterSet * underscoreCharSet = [NSCharacterSet characterSetWithCharactersInString:#"_"];
NSRange underscoreRangeOne = [startOne rangeOfCharacterFromSet:underscoreCharSet];
NSRange underscoreRangeTwo = [startTwo rangeOfCharacterFromSet:underscoreCharSet];
if (underscoreRangeOne.length > 0 || underscoreRangeTwo.length > 0) {
// Something is underscored, put it on the bottom
return (underscoreRangeOne.length > 0) ? NSOrderedDescending : NSOrderedAscending;
}
// Prioritize numbers to bottom
NSRange decimalRangeOne = [startOne rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
NSRange decimalRangeTwo = [startTwo rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
if (decimalRangeOne.length > 0 || decimalRangeTwo.length > 0) {
// Something is numbered, put it on the bottom
if (decimalRangeOne.length == decimalRangeTwo.length) {
return (startOne.intValue > startTwo.intValue) ? NSOrderedDescending : NSOrderedAscending;
}
else if (decimalRangeOne.length > decimalRangeTwo.length) {
return NSOrderedDescending;
}
else if (decimalRangeTwo.length > decimalRangeOne.length) {
return NSOrderedAscending;
}
}
// Now, sort alphabetically
return [nameOne localizedCaseInsensitiveCompare:nameTwo];
}];
NSLog(#"SortedArray: %#", sortedArray);
Will log as:
abcd,
Anna,
01Bob,
02Tob,
03ZED,
"04_rob",
191anna,
"_anna",
"_bob",
"_boc",
"_bocd12",
"_bocd13",
"_01Bob",
"_02Tob"
You can optimize this further, but your sort logic would be like below.
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *name1 = [(NSDictionary *) obj1 objectForKey:NAME];
NSString *name2 = [(NSDictionary *) obj2 objectForKey:NAME];
if ([name1 characterAtIndex:0] == '_' && [name2 characterAtIndex:0] == '_')
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
else if ([name1 characterAtIndex:0] == '_')
{
return NSOrderedDescending;
}
else if ([name2 characterAtIndex:0] == '_')
{
return NSOrderedAscending;
}
else if (([name1 intValue] && [name2 intValue]) || ([name1 characterAtIndex:0] == '0' && [name2 characterAtIndex:0] == '0'))
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
else if ([name1 intValue] >0 || [name1 characterAtIndex:0] == '0')
{
return NSOrderedDescending;
}
else if ([name2 intValue]>0 || [name2 characterAtIndex:0] == '0')
{
return NSOrderedAscending;
}
else
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
//return res;
}];
I am trying to search an NSMutableArray for a string. I am using containsObject: currently but it looks like this is case sensitive. I need to search for all combinations of the given string (trip). Any ideas would be greatly appreciated.
if ([self.theArray containsObject:trip]) {
}
Not that hard:
BOOL found = NO;
for (NSString* str in self.theArray) {
if ([str caseInsensitiveCompare:trip] == NSOrderedSame) {
found = YES;
break;
}
}
Create a category for NSArray and add this function in there.
- (BOOL)containsStringCaseInsensitive:(NSString *)key {
key = [key lowercaseString];
for (int i = ([self count] - 1); i >= 0; i--) {
NSObject * obj = [self objectAtIndex:i];
if ([obj isKindOfClass:[NSString class]]) {
NSString * strInArray = [(NSString *)obj lowercaseString];
if ([key isEqualToString:strInArray]) {
return YES;
}
}
}
return NO;
}
How about a block:
__block trip = #"blah";
if (NSNotFound!=[self.theArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop)
{
if (NSOrderedSame==[(NSString *)obj caseInsensitiveCompare:trip])
{
stop=YES;
return YES;
}
return NO;
}])
{
NSLog(#"It's a MATCH!");
}