Sorting IBOutletCollection Array of TextFields According to frame.origin.y - iphone

I'm trying to sort array of textFields according to frame.origin.y. But when I ran simulator it got stuck. Any Idea?
IBOutletCollection(UITextField) NSArray *textFields
My code :
-(NSMutableArray *)bubbleSort:(NSMutableArray *) unsortedArray{
NSInteger i,j;
//NSLog(#"%#",unsortedArray);
for(i=0;i<unsortedArray.count;i++)
{
for(j=0;j<i;j++)
{
if(((UITextField *)[unsortedArray objectAtIndex:i]).frame.origin.y > ((UITextField *)[unsortedArray objectAtIndex:j]).frame.origin.y)
{
UITextField *temp=[unsortedArray objectAtIndex:i];
[unsortedArray insertObject:[unsortedArray objectAtIndex:j] atIndex:i];
[unsortedArray insertObject:temp atIndex:j];
}
}
}
//NSLog(#"%#",unsortedArray);
return unsortedArray;
}

Like Learner said, you just keep on inserting object in your array, so you never leave your outer for loop. To swap two elements, you can use
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
so in your case, inside your if, you can simply write
[unsortedArray exchangeObjectAtIndex:i withObjectAtIndex:j];
Reference

look like this will keep on inserting object in array. so unsortedArray.count will keep increasing. in my opinion you need to remove object first and than insert it to upper rank or lower rank depending on algorithm.
se documentation for NSMutableArray insert object function.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

Related

NSInteger value not valid when passing in a variable, for custom table view class delegate method

I'm testing a custom table view style class:
HorizontalTable
It produces a horizontal table view.
One of the delegate methods equivalent to tableView:numberOfRowsInSection: is:
- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView.
If I give this a number (ex: return 10;) it is happy and it give me the number "cells" that I want. But if I feed it a value of someArray.count or an int or NSInteger variable, the table view just comes out blank, delivering no cells.
I think that the method in the custom table view class that receives the NSInteger value is this:
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate)
numPages = [_delegate numberOfColumnsForTableView:self];
return numPages;
}
Do I need to cast the result of someArray.count to an NSInteger?
Here you get the value from array means your array not nil so just debug and check the if condition that its come in that condition or not and what you get from NSLog
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate){
numPages = [_delegate numberOfColumnsForTableView:self];
NSLog(#"Total record %d",numPages);//what you get here?
}
return numPages;
}
numPages = [_delegate numberOfColumnsForTableView:self];
//self requires an object of type HorizontalTableView
NSMutableArray is editable, where as NSArray is read-only.
NSMutableArray is a subclass of NSArray and responds to messages such as addObject, removeObject and so forth; i.e. it is mutable, like the name says. Instead, NSArray is immutable, i.e. you can't add/remove objects.
In fact converting the value to be returned to the count of an NSArray, as opposed to the count an NSMutableArray fixes the issue. Why? I am not sure why an NSArray's count value is valid but not the NSMutableArray's. Anyone?

Check if something exists in an NSMutableArray

I have an NSMutableArray which can hold several objects. I would like to check if an object exists, and if so, alter it. I was wondering about checking it. I thought this would work:
if ([[[self.myLibrary objectAtIndex:1] subObject] objectAtIndex:1]) // do something
However, this will crash if there aren't any subObjects at index 1.
So I guess the problem is that the above does not return nil if there isn't anything at this Index.
Is there another easy way to check or will I have to count through the array etc.? I know there are other posts on stackoverflow on this, but I haven't found a simple answer yet.
Any explanations / suggestions welcome. Thanks!
No check simply using :
[myArray indexOfObject:myObject];
or
[myArray containsObject:myObject];
These methods check every object using isEqual.
For example:
NSUInteger objIdx = [myArray indexOfObject: myObject];
if(objIdx != NSNotFound) {
id myObj = [myArray objectAtIndex: objIdx];
// Do some alter stuff here
}
If this is a pattern you use a lot, you could add a category to NSArray called something like safeObjectAtIndex:, which takes an index, does the bounds checking internally:
-(id)safeObjectAtIndex:(NSUInteger)index {
if (index >= [self count])
return nil;
return [self objectAtIndex:index];
}
Assuming the object you are using to search with and the actual object in the array are the exact same instance, and not two different objects that are equal according to an overridden isEqual: method, you can do this:
if ([array containsObject:objectToSearchFor]) {
// modify objectToSearchFor
}
If the two objects are different instances which are equal according to isEqual:, you will have to use code like this:
NSUInteger index = [array indexOfObject:objectToSearchFor];
if (index != NSNotFound) {
id objectInArray = [array objectAtIndex:index];
// modify objectInArray
}
NSArray (which is the NSMUtableArray superclass) has lots of methods for finding objects. Have a look at the documentation.
You can either rely on the equals method (e.g. indexOfObject:) or provide a block (e.g indexOfObjectPassingTest:) which is pretty funky.
It's fairly common in Objective C to be using the Mutable version of a class but rely on methods in the non mutable superclass so it's always a good idea when checking the online documentation to look at the superclass.

Sorting an NSMutableArray containing matching "pairs" of values

I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView. The odd number values are "user names", and the even number values are their matching "dates". (The "pairs" must remain together.)
How would I sort "by user name"?
How would I sort "by date"?
Should I be using sortUsingFunction or sortUsingSelector?
-(void) test
{
NSMutableArray *anArray = [NSMutableArray arrayWithObjects:#"Zeke", #"01-Jan-2010", #"Bob", #"02-Jan-2010", #"Fred", #"03-Jan-2010", #"Susan", #"04-Jan-2010", #"Kim", #"05-Jan-2010", #"Debbie", #"06-Jan-2010", nil];
[anArray sortUsingFunction:SortByDate context:(void *)context];
}
NSComparisonResult SortByDate(NSMutableArray *a1, NSMutableArray *a2, void *context)
{
// What goes here to keep the pair-values "together"?
}
Why aren'e you using a NSDictionary? You can have user names as keys and the dates as corresponding values. Then if you want to sort on user names, just use [dictObj allkeys] to get an array containing just the keys. Sort them as you prefer and then when displaying display the sorted array of keys and fetch the corresponding value for the key and display along side. Similarly, you can get all the values using [dictObj allValues] sort them and display them along with keys.
I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView
Does that mean cells alternate between displaying usernames and dates, or are you multiplying/dividing by 2?
Either way, you need to fix your model. The bit where you go "oops, sorting doesn't work" should be a big hint that you haven't chosen a good representation of your data. Adding a workaround will only lead to more tears later.
I'd suggest something like this:
Create a class to represent your (username,date) tuple.
Add -compareByDate: and -compareByUsername: methods.
[array sortUsingSelector:#selector(compareByDate:)]
If you want to display usernames and dates in alternating cells, then it's still easy to do, but I'd simply use a taller cell and save myself some pain.
Note: The overhead of Objective-C method calls means that you can get a significant performance increase by using sortUsingComparator: and defining appropriate functions (you can stick the function between #implmentation and #end to let them access protected/private ivars) but it's not really worth doing this unless you've determined that sorting is a bottleneck.
Something like this could work:
#class SortWrapper: NSObject
#property (copy,readwrite,atomic) NSString* name;
#property (copy,readwrite,atomic) NSDate* date;
#end
...
- (void)test
{
NSArray* names = #[#"Alice", #"Bob", #"Cameron"];
NSArray* dates = #[aliceDOB, bobDOB, cameronDOB]; // NSDate objects
// turn into wrapped objects for sorting
NSMutableArray* wrappedObjects = [NSMutableArray array];
for ( int i=0; i<names.count; i++ ) {
SortWrapper* wrapped = [[SortWrapper alloc] init];
wrapped.name = names[i];
wrapped.date = dates[i];
[wrappedObjects addObject:wrapped];
}
// to sort by date:
NSArray* sortedByDate = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
return [obj1.date compare:obj2.date];
}];
// to sort by name:
NSArray* sortedByName = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
return [obj1.name compare:obj2.name];
}];
}

Convert a string + a number to an onject

I am new to Obj C, but familiar with OOP. I am working on a game that uses collision detection to score points and change UIImageView properties like background color.
I have a finite number of UIImageViews that are subviews of a larger view. I can manually check for collision and set the background colors of the UIImageViews accordingly. The problem is that this creates a rather large if / else block that I can see growing out of control. In JavaScript I can use a for loop and eval() to evaluate a string + i (the loop var) and then simply call the new var.backgroundColor = someColor;
It seems that eval is not used in OBJ C, so I am looking for an equivalent that will allow me to create a loop that executes the same block of code on a given object that only differs from other objects by name only:
Example:
if (someStatement == true){
object_01.someProperty = newProperty;
} else if (someOtherStatement == true){
object_02.someProperty = newProperty;
} else if (someOtherOtherStatement == true){
object_03.someProperty = newProperty;
}
I want to write the above as a loop. I realize I can use an Array, but if so, how? Also, I want to loop through CGRects which are not storing properly in an Array.
Any help greatly appreciated!
Damon
Creating your array:
NSMutableArray *items = [NSMutableArray array];
Populating your array with CGRects:
[items addObject:[NSValue valueWithCGRect:frame]];
Pulling a CGRect from the array:
CGRect frame = [[items objectAtIndex:0] CGRectValue];
Replacing an index in the array:
[items replaceObjectAtIndex:0 withObject:[NSValue valueWithCGRect:frame]];
The best way is to put object_01, object_02, ... into an array and then use `-[NSArray objectAtIndex:] to access the objects.
You could also use Key-Value Coding:
NSString *keyPath = [NSString stringWithFormat:#"object_0%d.someProperty", objectIndex];
[self setValue:newProperty forKeyPath:keyPath];
But as rpetrich mentioned, it's not good programming style.

iphone indexed table view problem

I have a table view in which I'm using sectionIndexTitlesForTableView to display an index. However, when I scroll the table, the index scrolls with it. This also results in very slow refreshing of the table. Is there something obvious I could be doing wrong? I want the index to remain in place on the right while the table scrolls. This is the code I'm using for the index titles:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:#"A"];
[tempArray addObject:#"B"];
[tempArray addObject:#"C"];
[tempArray addObject:#"D"];
...
return tempArray;
}
You really should be creating the index list somewhere else (say, in your table controller's init or loadView methods) and retaining it as an instance variable for later use. Then in sectionIndexTitlesForTableView you only have to return that ivar. If it isn't a property with a retain attribute then make sure you retain it when created so it sticks around (and release it in dealloc).
An easy way to create it is:
self.alphabetIndex = [NSArray arrayWithArray:
[#"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:#"|"]];
The actual letters would have to change depending on the language locale setting but this way it's a bit easier to localize.
You definitely don't want to be creating that temp array each time because it's going to get called a lot.
As far as the index scrolling away it may be related to your returning a new array each time. Try the above first and if it doesn't solve the problem then you may want to tweak the value for the table's sectionIndexMinimumDisplayRowCount property and see if it makes any difference.
I would avoid creating a new NSMutableArray and releasing it every time. Try creating those on viewDidLoad or the class constructor and just reference the pre-built array on sectionIndexTitesForTableView.
If you are not manipulating the array at all, you probably don't need the overhead of an NSMutableArray at all. Try switching it to a plain old NSArray by using the arrayWithObjects static autorelease constructor.
That should speed things up for you.
Make a static variable, it will be released on app exit.
static NSMutableArray* alphabet = nil;
+ (void)initialize {
if(self == [MyViewController class]){
NSUInteger const length = 'Z' - 'A' + 1;
alphabet = [[NSMutableArray alloc] initWithCapacity:length];
for(NSUInteger i=0; i<length; ++i){
unichar chr = 'A' + i;
[alphabet addObject:[NSString stringWithCharacters:&chr length:1]];
}
}
}