Here i used this code to view the next value in the array,
-(IBAction)changenext
{
static int j = 0;
if (j >arcount)
{
j = 0;
}
lb1.text=[NSString stringWithFormat:[artext objectAtIndex:j]];
imager.image=[arimage objectAtIndex:j];
aplayer=[[AVAudioPlayer alloc]initWithData:[arsound objectAtIndex:j] error:nil];
j++;
}
How to view the previous array value by other button click?Please help me to solve..
-(IBAction)change_next_or_prev:(id)sender
{
static int j = 0;
if(sender == btnNext)
j++;
else if(sender == btnPrev)
j--;
if (j >= arcount)
{
j = 0;
}
else if(j < 0)
{
j = arcount - 1;
}
lb1.text=[NSString stringWithFormat:[artext objectAtIndex:j]];
imager.image=[arimage objectAtIndex:j];
aplayer=[[AVAudioPlayer alloc]initWithData:[arsound objectAtIndex:j] error:nil];
}
link both button to the same action.
-(IBAction)changeprev
{
static int j = arcount;
if (j < 0)
{
j = arcount;
}
lb1.text=[NSString stringWithFormat:[artext objectAtIndex:j]];
imager.image=[arimage objectAtIndex:j];
aplayer=[[AVAudioPlayer alloc]initWithData:[arsound objectAtIndex:j] error:nil];
j--;
}
Related
I have two NSMutableArrays. Each contains a custom word object in it. Custom word has 2 properties text and frequency. Now I want to combine these two arrays in such a way that, if these two arrays has same text in it, then it should compare the frequency of those two text and select the highest frequency of the two. And also it should remove the duplicates from the array.
I tried every logic for this but was not able to do this. Can any body help me with the logic for this. Following the code. But it should also remove the duplicates.
for (int i = 0; i < [array count]; i++) {
for (int j = 0; j < [array count]; j++) {
if ([[[array objectAtIndex:i]firstWord] isEqualToString:[[array objectAtIndex:j] firstWord]]) {
if ([[array objectAtIndex:i] frequency] < [[array objectAtIndex:j] frequency]) {
CustomWordFrequency *word = [array objectAtIndex:i];
word.frequency = [[array objectAtIndex:j] frequency];
[array replaceObjectAtIndex:i withObject:word];
}
}
}
}
Combine the two arrays.
Sort the resulting array by text ASC, frequency DESC
Loop through the array
a. Look at the next item in the array. If the text is the same as the current word, remove it from the array. If it's different, continue looping.
NSArray *combined = [firstArray arrayByAddingObjectsFromArray:secondArray];
[combined sortUsingComparator:^(id firstObject, id secondObject) {
NSComparisonResult *result = [firstObject.text compare:secondObject.text];
if (result == NSOrderedAscending) return NSOrderedAscending;
if (result == NSOrderedDescending) return NSOrderedDescending;
if (result == NSOrderedSame) {
result = [firstObject.frequency compare:secondObject.frequency];
if (result == NSOrderedAscending) return NSOrderedDescending;
if (result == NSOrderedDescending) return NSOrderedAscending;
if (result == NSOrderedSame) return NSOrderedSame;
}
}];
for (int i = 0; i < [combined count] - 2; ++i) {
CustomWordFrequency *word = [combined objectAtIndex:i];
int j = i + 1;
while ([word.text compare:[combined objectAtIndex:j].text == NSOrderedSame) {
[combined removeObjectAtIndex:j];
j++;
if (j == [combined count]) {break;}
}
if (i >= [combined count] - 2) {break;} // the count keeps changing so check here
}
NSMutableArray *combinedArray = [[NSMutableArray alloc] init];
BOOL flagForMatchFound = FALSE;
for(CustomWordFrequency *firstWord in firstArray)
{
flagForMatchFound = FALSE;
for(CustomWordFrequency *secondWord in secondArray)
{
if([firstWord.firstWord isEqualToString:secondWord.firstWord])
{
if(firstWord.frequency >= secondWord.frequency)
{
[combinedArray addObject:firstWord];
flagForMatchFound = TRUE;
}
else
[combinedArray addObject:secondWord];
}
else
{
if(!flagForMatchFound)
[combinedArray addObject:secondWord];
}
}
}
I have a for loop running on string length. the first loop looks for a character common in the second loop. I want to save the index of nameString that is common in an array or a string. Please help. Code is mentioned below.
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
NSRange range;
range.length=1;
range.location=j;
[partnersNameString replaceCharactersInRange:range withString:#""];
calculateField.text = partnersNameString;
}
}
}
There are several ways to go about doing what you are looking for: you can use a "plain" C array with a count, or you can use an NSMutableArray with wrappers.
The first way would look like this:
NSUInteger indexes[MAX_LENGTH];
NSUInteger lastIndex = 0;
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
indexes[lastIndex++] = i;
// Go through the rest of your code
...
}
}
}
for (NSUInteger i = 0 ; i != lastIndex ; i++) {
NSLog(#"Found match at index %u", indexes[i]);
}
The second way looks similar, except now you need to use NSNumber to wrap the data going into NSMutableArray:
NSMutableArray *indexes = [NSMutableArray array];
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
[indexes addObject[NSNumber numberWithInt:i]];
// Go through the rest of your code
...
}
}
}
for (NSNumber in indexes) {
NSLog(#"Found match at index %i", [[indexes objectAtIndex:i] intValue]);
}
You can use NSMutablearray for that
NSMutableArray myArray = [NSMutableArray new];
for (int i=0; i < nameString.length; i++) {
char currentLetter = [nameString characterAtIndex:i];
for (int j=0; j < partnersNameString.length; j++) {
if (currentLetter == [partnersNameString characterAtIndex:j]) {
//And add it here
[myArray addObject:currentLetter];
NSRange range;
range.length=1;
range.location=j;
[partnersNameString replaceCharactersInRange:range withString:#""];
calculateField.text = partnersNameString;
}
}
}
[myArray addObject:[NSNumber numberWithInt:i]];
How to get check change the button image if it reach the last element of the array and perform to start from first on click that button?
Here my code,Please help me
-(IBAction)changenext:(id)sender
{
static int j = 0;
backimg.enabled=TRUE;
if(sender == nextimg)
j++;
else if(sender == backimg)
j--;
if (j >= arcount)
{
j = 0;
}
else if(j < 0)
{
j = arcount - 1;
}
imager.image=[arimage objectAtIndex:j];
}
What change should i made?
Hi i can not clearly understand what you asking but simply you get last element of NSArray with this code :-
[myArray lastObject]
For Example:-
NSString *str = [myArray lastObject]
Can I transform this loop in a while loop ?
int i=0;
for (NSDictionnary *crossArrayDictionnary in (NSArray *)mainArray) {
//some code...
i++;
if (i>=50) {
break;
}
}
Thanks,
This should do the trick:
NSInteger max, i = 0;
max = [mainArray count] < 50 ? [mainArray count] : 50;
while (i < max) {
NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i];
// You code here.
i++;
}
NSEnumerator *enumerator = [mainArray objectEnumerator];
NSDictionary *crossArrayDictionnary = nil;
while ((crossArrayDictionnary = [enumerator nextObject])) {
//some code...
i++;
if (i>=50) {
break;
}
}
As rckoenes said,
Minor change.
NSInteger i = 0;
while (i < 50 && [itemsMutable cout] >= i) {
NSDictionary *crossArrayDictionnary = [itemsMutable objectAtIndex:i];
// You code here.
i++;
}
don't know objective-c, but something like this:
int i=0;
while (i < 50 && i < [mainArray count]) {
NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i];
//some code...
i++;
}
I have copy JSON.framework in my
Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/System/Library/Frameworks
both folder and importing
#import <JSON/SBJsonParser.h>
but when I am using it giving me linking error error
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
Ld build/Debug-iphonesimulator/HelloThere.app/HelloThere normal i386
cd /Users/samargupta/Desktop/hellothere
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk -L/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -F/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -filelist /Users/samargupta/Desktop/hellothere/build/HelloThere.build/Debug-iphonesimulator/HelloThere.build/Objects-normal/i386/HelloThere.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -framework CoreLocation -framework MapKit -framework JSON -o /Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator/HelloThere.app/HelloThere
ld: framework not found JSON
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
plz tell me where I am wrong??
I wrote a JSON-parser by myself. You can use it if you want to by copying the code into your class source.
You use it in this way:
NSObject *obj = [self jsonToObj:yourJsonString];
It returns a NSDictionary, NSString or NSArray depending on the input. The difference between my parser and others is, that it also accepts numerical keys, which are not allowed in the JSON-standard. I can't guarantee that there are no bugs in it or that it works with all JSON-strings. Please give me some feedback, if it works for you!
-(NSObject*)jsonToObj:(NSString *)json {
int nextOffset;
return [self jsonToObj:json offset:0 nextOffset:&nextOffset];
}
static int indexOfChar(NSString *str, unichar c, int offset) {
int length = [str length];
for (int i = offset; i < length; i++) {
if ([str characterAtIndex:i] == c) {
return i;
}
}
return -1;
}
static int indexOfNonWhitespace(NSString *str, int offset) {
int len = [str length];
NSCharacterSet *cSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for (int i = offset; i < len; i++) {
if (![cSet characterIsMember:[str characterAtIndex:i]]) {
return i;
}
}
return -1;
}
static int indexOfNonNumber(NSString *str, int offset) {
int len = [str length];
for (int i = offset; i < len; i++) {
unichar c = [str characterAtIndex:i];
if (c != '-' && c != '.' && (c < '0' || c > '9')) return i;
}
return -1;
}
static int parseHex(NSString *hexStr) {
unsigned int result = 0;
int len = [hexStr length];
for (int i = 0; i < len; i++) {
result <<= 4;
unsigned int v = 0;
unichar c = [hexStr characterAtIndex:i];
if (c >= '0' && c <= '9') {
v = c-'0';
} else if (c >= 'a' && c <= 'f') {
v = c-'a'+10;
} else if (c >= 'A' && c <= 'F') {
v = c-'A'+10;
}
result += v;
}
return result;
}
-(NSObject*)jsonToObj:(NSString *)json offset:(int)offset nextOffset:(int*)nextOffset {
offset = indexOfNonWhitespace(json, offset);
static NSString *jsonExceptionName = #"InvalidJSONException";
if (offset == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 1" userInfo:nil];
}
unichar startChar = [json characterAtIndex:offset];
if (startChar == '{') {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
int curPos = offset+1;
while (YES) {
curPos = indexOfNonWhitespace(json, curPos);
if (curPos == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 2" userInfo:nil];
}
unichar curChar = [json characterAtIndex:curPos];
if (curChar == ',') {
curPos++;
continue;
} else if (curChar == '}') {
*nextOffset = curPos+1;
return result;
}
unichar quotChar = curChar;
if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:curPos-1]=='\\') {
quotChar = 0;
}
NSString *key = nil;
int semiIndex = 0;
if (quotChar != 0) {
int quotStart = curPos+1;
int quotEnd = quotStart;
while (YES) {
quotEnd = indexOfChar(json, quotChar, quotEnd);
if (quotEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"quotation-end not found 1" userInfo:nil];
}
if ([json characterAtIndex:quotEnd-1] != '\\') break;
else quotEnd++;
}
key = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)];
semiIndex = indexOfChar(json, ':', quotEnd);
if (semiIndex == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"semicolon not found 1" userInfo:nil];
}
} else {
semiIndex = indexOfChar(json, ':', curPos);
if (semiIndex == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"semicolon not found 2" userInfo:nil];
}
key = [[json substringWithRange:NSMakeRange(curPos, semiIndex-curPos)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
[result setObject:[self jsonToObj:json offset:semiIndex+1 nextOffset:&curPos] forKey:key];
}
} else if (startChar == '[') {
NSMutableArray *result = [NSMutableArray array];
int curPos = offset+1;
while (YES) {
curPos = indexOfNonWhitespace(json, curPos);
if (curPos == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 3" userInfo:nil];
}
unichar curChar = [json characterAtIndex:curPos];
if (curChar == ',') {
curPos++;
continue;
} else if (curChar == ']') {
*nextOffset = curPos+1;
return result;
}
[result addObject:[self jsonToObj:json offset:curPos nextOffset:&curPos]];
}
} else {
unichar quotChar = startChar;
if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:offset-1]=='\\') {
quotChar = 0;
}
if (quotChar != 0) {
int quotStart = offset+1;
int quotEnd = quotStart;
while (YES) {
quotEnd = indexOfChar(json, quotChar, quotEnd);
if (quotEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"quotation-end not found 2" userInfo:nil];
}
if ([json characterAtIndex:quotEnd-1] != '\\') break;
else quotEnd++;
}
*nextOffset = quotEnd+1;
NSString *str = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)];
str = [str stringByReplacingOccurrencesOfString:#"\\\"" withString:#"\""];
NSMutableString *result = [NSMutableString stringWithCapacity:[str length]];
int len = [str length];
for (int i = 0; i < len; i++) {
unichar c = [str characterAtIndex:i];
if (c != '\\') {
[result appendString:[NSString stringWithCharacters:&c length:1]];
} else if (i+1 < len) {
unichar c2 = [str characterAtIndex:i+1];
if (c2 == '\\') {
[result appendString:[NSString stringWithCharacters:&c2 length:1]];
i++;
} else if (c2 == 'u') {
unichar cUni = parseHex([str substringWithRange:NSMakeRange(i+2, 4)]);
[result appendString:[NSString stringWithCharacters:&cUni length:1]];
i += 5;
}
}
}
return result;
} else {
int numberEnd = indexOfNonNumber(json, offset);
if (numberEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"number-end not found" userInfo:nil];
}
//BOOL isFloat = indexOfChar(json, '.', offset)!=-1;
NSString *numberStr = [json substringWithRange:NSMakeRange(offset, numberEnd-offset)];
*nextOffset = numberEnd;
return numberStr;
}
}
}
Just create a folder in your XCode project name it say "JSON". All the .h and .m files provided by the JSON framework must be in that and well include them in your app, build, run etc etc. Thats how.
The framework folder 'JSON.framework' must be added under each SDK framework folder that you wish to use (iPhone Simulator, iPhoneOS 4.3), or your build will fail with the error that you reported. Alternatively you can just have the folder linked out of your project folder as Bourne mentioned. Either way it Xcode needs to know where the framework is for each type of build your compiling.