At present if the sortdescriptor is having nil or empty values is being placed in an untitled section which is being placed at the top of the table. I want it to be at the end of the table. Any suggestions?
yes, it is so easy, jst perform a segmentation in which start by the charecter A and check upto z, (or whatever your requiremtn) if it matches nothing, then add it to last array that you are going to show in untititled objects. i have this for contacts. see if it is understandable by u
int numContacts=[cList count];
//NSMutableArray *nonAlphaArray=[[NSMutableArray alloc] init];
NSMutableArray *arrayCollection[27];
for (int i=0; i<27; i++) {
arrayCollection[i]=[NSMutableArray array];
}
for (int i=0; i<numContacts; i++)
{
Contact *contact= [cList objectAtIndex:i];
unichar alphaSmall='a';
unichar alphaBig='A';
unichar first=0x0000;
if([contact.mContactName length]>0)
first= [contact.mContactName characterAtIndex:0];
for (int j=0; j<26; )
{
if (first==alphaSmall || first==alphaBig)
{
[arrayCollection[j] addObject:contact];
break;
}
alphaSmall++;
alphaBig++;
j++;
if (j==26) {
[arrayCollection[26] addObject:contact];
}
}
}
for (int i=0; i<27; i++)
{
[alphaDictionary setObject:arrayCollection[i] forKey:[NSString stringWithFormat:#"%d",i]];
}
Related
I want to below effect,but I don't kown how to use NSMutableArray combine NSArray More than two?
1.my code
for (int i=0; i<[DateSortArry2 count]; i++) {
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[Arry addObject:sectiondateStr2];
}
}
[SumArry addObjectsFromArray:Arry];
[Arry removeAllObjects];
}
2.my code Result
SumArry:(
"20130227",
"20130227",
"20130227",
"20130226",
"20130226",
"20130226",
"20130225",
"20130225")
3.I want the results
SumArry:((
"20130227",
"20130227",
"20130227",
),
(
"20130226",
"20130226",
"20130226",
),
(
"20130225",
"20130225"
))
Your code repeatedly fills and empties the same array by adding its elements, but you need to preserve the structure with additional instances of NSArray. So, use a new NSArray for each section.
for (int i=0; i<[DateSortArry2 count]; i++) {
NSMutableArray *section = [NSMutableArray array];
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[section addObject:sectiondateStr2];
}
}
[SumArry addObject:section];
}
You can either store a reference to another array (or any type of object) in your array:
[myMutableArray addObject:otherArray];
Or concatinate the arrays.
[myMutableArray addObjectsFromArray:otherArray];
Both of which are documented in the documentation. By the looks of it the first approach is what you want since you want to have NSArray of NSMutableArray.
try this:
please tell me if it works.
thanks
NSString *str = #"";
for (int i=0; i<[DateSortArry2 count]; i++)
{
if (str isEqualToString:[DateSortArry2 objectAtIndex:i])
{
return;
}
else
{
NSMutableArray * Arry = [[NSMutableArray alloc] init];
str = [DateSortArry2 objectAtIndex:i]
for (int j=0; j<[DateSortArry2Copy count]; j++)
{
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:str])
{
[Arry addObject:str];
}
}
[SumArry addObject:Arry];
[Arry removeAllObjects];
}
}
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]];
I have solved the problem with including terminating method.
If(indexes==0){
endUp = YES;
}
Thanks for pointing me the right direction. It wasn't problem of the shuffle.
Alessign
the error may be in the loop you use to shuffle the first array.
You don't post that part of your code...is it something like this?
for (int i=0; i<[indexes count]; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this may be better:
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this complete code works well, with no errors or crashes:
int length = 10;
NSMutableArray* indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<10; i++) [indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray*shuffle = [[NSMutableArray alloc] initWithCapacity:length];
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
int index = arc4random()%[indexes count];
NSLog(#"___index: %i", index);
NSLog(#"indexes: %# ", indexes);
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
NSLog(#"shuffle: %# ", shuffle);
}
for (int i=0; i<[shuffle count]; i++){
int questionNumber = [[shuffle objectAtIndex:i] intValue] + 1;
NSLog(#"questionNumber: %i ", questionNumber);
}
I see, anyway, Got it! I implemented one 'if' statement which will terminate it!
if ([indexes count] == 0)
{endProcess = YES}
I have an array of Place objects. Each Place object has a name and code property, both strings. Each Place object already has a code, but I need to look up the name property from a server. I get back 2 arrays: one contains name, the other codes. These arrays are ordered so that the name at some index in the nameArray corresponds exactly with the code at the same index of the codeArray.
I have been looping through the array of Place objects, then checking to see if the code property for that Place is the same as the current index in the codeArray. If it is, I set the name of that Place by using the same index in the nameArray:
for(int i = 0; i < [placesArray count]; i++)
{
for(int j = 0; j < [codeArray count]; j++) {
if([[[placesArray objectAtIndex:i] code] isEqualToString:[codeArray objectAtIndex:j]]) {
[[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
}
}
}
This works but isn't terribly fast - it can take 30 seconds with 1000+ Places to loop through.
Is there a faster way?
As with anytime you're trying to optimize performance, you should profile the code using Instruments to find out where the bottleneck actually is. That said, looping through the placesArray for each name in the nameArray and doing a string comparison is pretty inefficient.
How about something like this?
NSMutableDictionary *placesByCode = [NSMutableDictionary dictionaryWithCapacity:[placesArray count]];
for (Place *aPlace in placesArray) {
[dictionary setObject:aPlace forKey:aPlace.code];
}
NSMutableDictionary *namesByCode = [NSMutableDictionary dictionaryWithCapacity:[namesArray count]];
for (int i=0; i<[namesArray count]; i++) {
NSString *name = [namesArray objectAtIndex:i];
NSString *code = [codeArray objectAtIndex:i];
[namesByCode setObject:name forKey:code];
}
for (NSString *code in namesByCode) {
Place *place = [placesByCode objectForKey:code];
place.name = [namesByCode objectForKey:namesByCode];
}
Looking up each place by its code in the dictionary should be quite a bit faster than manually looping through the whole place array for each name.
You can use for NSArray -containsObject
if ([myarray containsObject:myObject]) {
// ...
}
Try using a break statement in the inner loop. This way you don't need to loop through the entire loop each time.
for(int i = 0; i < [placesArray count]; i++)
{
for(int j = 0; j < [codeArray count]; j++) {
if([[[placesArray objectAtIndex:i] code] isEqualToString:[codeArray objectAtIndex:j]]) {
[[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
break;
}
}
}
You could also make the second array become smaller as you find more results. It will cost you more memory but 1000 strings isn't much anyway.
NSMutableArray * tempCodeArray = [NSMutableArray arrayWithArray:codeArray];
for(int i = 0; i < [placesArray count]; i++)
{
for(int j = 0; j < [tempCodeArray count]; j++) {
if([[[placesArray objectAtIndex:i] code] isEqualToString:[tempCodeArray objectAtIndex:j]]) {
[[placesArray objectAtIndex:i] setName:[nameArray objectAtIndex:j]];
[tempCodeArray removeObjectAtIndex:j];
break;
}
}
}
The problem was not the counting for the array, it's the embedded for loop which will take O(n*n) and in Andrew's solution, it's only O(n)+O(n)+O(n)+whatever take to find a object of the key in the dictionary, which i guess would be in a hash table lookup and that's really fast.
Colby, you probably will be ok with Andrew's solution. If you still wanna improve the performance, then a good idea would be sort the array's first then do lookup.
Hope this helps.
I want to compare an array containing int values to a number
example [1,2,30,1,40,200,10,500] < 500 meaning if array contains elements less than 500.
How can i do dis?
Thank alot in advance
I did this bt it say invalid opernd:
if ([placeName count])
{
for (int i =0; i < [placeName count]; i++)
{
NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];
self.placeName = [NSMutableArray arrayWithArray:sortedArray];
NSMutableArray *tempArray = [sortedArray objectAtIndex:i];
NSDecimalNumber *Distance = [tempArray objectForKey:#"distance"];
NSMutableArray *intDistanse=[Distance intValue];
DLog(#"Distance%d", intDistanse);
NSMutableArray *intDistanse=[Distance intValue];
DLog(#"Distance%d", intDistanse);
for(int j = 0; j < [intDistanse count]; j++)
{
if ( intDistanse[j] < 500 ) //here its says error
{
DLog(#"success");
}
}
}
}
What kind of an array? NSArray or language array?
Easiest way is a loop. Without knowing the type of the array, this is pseudo code.
for(int i = 0; i < arrayCount; i++)
if ( array[i] < 500 )
.... got an element less than 500 ....
The code doesn't really make sense:
that code should be generating a ton of compiler warnings; every warning indicates a bug that should be fixed. Also, try "Build and Analyze".
you are sorting an array every time through the loop; waste of resources and doesn't make sense
everything being typed as NSMutableArray* doesn't make sense; do you really have an array of arrays of arrays?
... calling objectForKey: on anything but an NSDictionary doesn't work
intValue returns an (int), not an NSMutableArray*
if intDistance is an (int), then it should just be ( intDistance < 500 )
Overall, I would suggest you step back and review the Objective-C language guide and a bunch of working examples.
Typically, you loop over each element in the array, checking if each element is less than 500.
For example:
int i;
for (i=0; i < THE_SIZE_OF_THE_ARRAY; ++i)
{
if (array[i] < 500)
{
/* Do something */
}
}
If you want to see whether there is at least one item that matches your condition:
bool found = false;
for(int i = 0; i < sizeof(example)/sizeof(example[0]); ++i)
{
if(example[i] < 500)
{
found = true;
break;
}
}
And if you want to check whether all items match your condition:
bool found = true;
for(int i = 0; i < sizeof(example)/sizeof(example[0]); ++i)
{
if(example[i] >= 500)
{
found = false;
break;
}
}
Did find a solution to it type casting:
NSMutableArray *tempArray = [sortedArray objectAtIndex:i];
//DLog(#"sortedArray%#", sortedArray);8=
NSNumber *DistanceNum = [tempArray objectForKey:#"distance"];
NSLog(#"distance%#:::",DistanceNum);
NSInteger intDistance = (int)[DistanceNum floatValue];
if(intDistance<500)
{
NSLog(#"h:)");
NSString *notifications =#"Yes";
[[AppHelper mDataManager] setObject:notifications forKey:#"notifications"];
NSLog(#"notifications:%#",notifications);
RemindMeViewController *object = [[RemindMeViewController alloc] initWithNibName:#"RemindMeViewController" bundle:nil];
// RemindMeViewController *object=[[RemindMeViewController alloc]initWithNibName];
NSLog(#"notifications set");
[object scheduleNotification];
}
You can do this very easily by just implementing the observer to compare. Following is just an example to implement the custom observer to compare
#implementation NSArray (KVO)
- (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
NSUInteger idx=[indexes firstIndex];
while(idx!=NSNotFound)
{
[[self objectAtIndex:idx] addObserver:observer
forKeyPath:keyPath
options:options
context:context];
idx=[indexes indexGreaterThanIndex:idx];
}
}
- (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath
{
NSUInteger idx=[indexes firstIndex];
while(idx!=NSNotFound)
{
[[self objectAtIndex:idx] removeObserver:observer
forKeyPath:keyPath];
idx=[indexes indexGreaterThanIndex:idx];
}
}
-(void)addObserver:(id)observer forKeyPath:(NSString*)keyPath options:(NSKeyValueObservingOptions)options context:(void*)context;
{
if([isa instanceMethodForSelector:_cmd]==[NSArray instanceMethodForSelector:_cmd])
NSRaiseException(NSInvalidArgumentException,self,_cmd,#"not supported for key path %# (observer was %#)", keyPath, observer);
else
[super addObserver:observer
forKeyPath:keyPath
options:options
context:context];
}
-(void)removeObserver:(id)observer forKeyPath:(NSString*)keyPath;
{
if([isa instanceMethodForSelector:_cmd]==[NSArray instanceMethodForSelector:_cmd])
NSRaiseException(NSInvalidArgumentException,self,_cmd,#"not supported for key path %# (observer was %#)", keyPath, observer);
else
[super removeObserver:observer
forKeyPath:keyPath];
}
#end