Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 20, 2012, 05:45 PM   #1
iJustinCabral
macrumors member
 
Join Date: Jul 2012
Need help with a UITextField method *help*

What I'm trying to do is make it so after they press done on the pop up keyboard, if the text is nil...no object is added to the array, but if its not nil then create the object and add it to the array.

Heres the code I have

Code:
 - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    if (!self.theList)
    {
        self.theList = [[NSMutableArray alloc] init];
    }
    
    Paper *newItem = [[Paper alloc]init];
    
    newItem.listName = textField.text;

if (newItem.listName == nil)
{
    [textField resignFirstResponder];
    [self.collectionView reloadData];
}    
   
else { 
        [self.theList addObject:newItem];
    
        textField.text = nil;
        
        NSLog(@"Count %d",self.theList.count);
        
        [textField resignFirstResponder];
   
        [self.collectionVIew reloadData];
    
}
    return YES;
}
__________________
CEO/Co-Founder Thinkr LLC.
iJustinCabral is offline   0 Reply With Quote
Old Aug 20, 2012, 06:10 PM   #2
Ides
macrumors member
 
Join Date: Mar 2012
What's wrong with the code you already have? In order for anyone to help you with something, you have to tell us what's not working.
Ides is offline   0 Reply With Quote
Old Aug 20, 2012, 06:11 PM   #3
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Why would the text ever be nill?

You should check if the string equals @"" (an empty string.) Actually, there might even be an NSString method to directly test if it's an empty string...
ArtOfWarfare is offline   0 Reply With Quote
Old Aug 20, 2012, 06:19 PM   #4
iJustinCabral
Thread Starter
macrumors member
 
Join Date: Jul 2012
I fixed the problem.


I just made an NSString = @"" and then compared my list object to the string and if it was equal then it wasn't added to the array
__________________
CEO/Co-Founder Thinkr LLC.
iJustinCabral is offline   0 Reply With Quote
Old Aug 20, 2012, 06:52 PM   #5
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: Sydney
You could also test the the Length of the string.

Code:
if (textView.text.length !=0)
{
//Do Stuff
}
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Aug 20, 2012, 07:45 PM   #6
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by MattInOz View Post
You could also test the the Length of the string.

Code:
if (textView.text.length !=0)
{
//Do Stuff
}
Testing the length is MUCH more efficient than string comparison. String comparison is fairly compute-intensive.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote
Old Aug 20, 2012, 08:33 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Duncan C View Post
Testing the length is MUCH more efficient than string comparison. String comparison is fairly compute-intensive.
I would be truly astounded if the string-comparison method doesn't test for length equality before comparing character-sequences.
chown33 is offline   0 Reply With Quote
Old Aug 20, 2012, 09:05 PM   #8
iJustinCabral
Thread Starter
macrumors member
 
Join Date: Jul 2012
ahhh I see. I'll change it up to do that, thanks a bunch.
__________________
CEO/Co-Founder Thinkr LLC.
iJustinCabral is offline   0 Reply With Quote
Old Aug 20, 2012, 10:45 PM   #9
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by chown33 View Post
I would be truly astounded if the string-comparison method doesn't test for length equality before comparing character-sequences.
How is length determined? I was under the impression that NSStrings weren't much different from C-strings, which would then mean length is determined by checking each character and seeing if it's a \0 or not... If it's going character by character either way, I don't see why length would be any faster?
ArtOfWarfare is offline   0 Reply With Quote
Old Aug 21, 2012, 12:10 AM   #10
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: Sydney
Quote:
Originally Posted by ArtOfWarfare View Post
How is length determined? I was under the impression that NSStrings weren't much different from C-strings, which would then mean length is determined by checking each character and seeing if it's a \0 or not... If it's going character by character either way, I don't see why length would be any faster?
From the Doc's for NSString
Quote:
The NSString class has two primitive methods—length and characterAtIndex:—that provide the basis for all other methods in its interface. The length method returns the total number of Unicode characters in the string. characterAtIndex: gives access to each character in the string by index, with index values starting at 0.
As a primative method it seem like it needs to fast and cheap.
Also it seems that all NSObjects maintain an Instance size in Bytes* it would seem like the length of the string in characters would be simple function of the size.

Not to mention that to comparing two strings you need two objects where as length is standalone.

*fun reading
MacDevCentre -inside Objective-c runtime
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Aug 21, 2012, 12:21 AM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by ArtOfWarfare View Post
I was under the impression that NSStrings weren't much different from C-strings, which would then mean length is determined by checking each character and seeing if it's a \0 or not... If it's going character by character either way, I don't see why length would be any faster?
A simple test to check this:
1. Create a string of 10 million characters.
2. Create a 2nd string that's the same 10 million characters plus an 'X'.
3. Create a 3rd string that's the same 10 million characters plus a 'Y'.
3. Perform isEqual: across the three strings.

If the length-first hypothesis is correct, it should be noticeably faster when the lengths differ, and noticeably slower when the lengths are the same. Any sufficiently large value would work, 10 million is just an example.
chown33 is offline   0 Reply With Quote
Old Aug 21, 2012, 07:19 PM   #12
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by chown33 View Post
A simple test to check this:
1. Create a string of 10 million characters.
2. Create a 2nd string that's the same 10 million characters plus an 'X'.
3. Create a 3rd string that's the same 10 million characters plus a 'Y'.
3. Perform isEqual: across the three strings.

If the length-first hypothesis is correct, it should be noticeably faster when the lengths differ, and noticeably slower when the lengths are the same. Any sufficiently large value would work, 10 million is just an example.
It seems to me that relying on the optimizations of a class's isEqual method is silly when there is a very clean way to test for an empty string.

The code:

Code:
if ([field.text length] == 0)
uses one of the primitive methods of the class directly to test that the string is empty. ANY other call you do (like isEqual or isEqualToString) at a minimum will have to call the length methods on both the field's text and the blank string you are passing in. If it is a naive implementation, it could be much, much worse.

Finally, checking that the length == 0 is really easy to read and maintain.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 02:53 AM
Best method for using 2 HD? OmarKanawati OS X 10.8 Mountain Lion 10 Jun 14, 2013 08:51 AM
Resolved: Add content form a UITextfield to an NSMutableArray everytime a button is clicked fstigre iPhone/iPad Programming 13 Nov 17, 2012 09:07 AM
UITextField Disappearing luke3 iPhone/iPad Programming 0 Jul 5, 2012 02:50 PM

Forum Jump

All times are GMT -5. The time now is 08:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC