Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
Hey guys, A question I am kind of stuck on. Is there anything better to use then an NSString for a small paragraph of text? So for example, a user inputs a few sentences then I want to store that text into a variable. Is the only thing I can use an NSString or is there something better and more effiecient? The reason I ask is because sometimes if the input is to big my program crashes.

Thanks a lot.
 

omenatarhuri

macrumors 6502a
Feb 9, 2010
902
844
You can always use plain C right? Just make sure the buffer is big enough and remember to null terminate and you shouldn't have a problem.

String streams are also very good, if you can use one.

(Not an expert on Objective C)
 

csnplt

macrumors 6502
Aug 29, 2008
320
1
Chicago Area
Hey guys, A question I am kind of stuck on. Is there anything better to use then an NSString for a small paragraph of text? So for example, a user inputs a few sentences then I want to store that text into a variable. Is the only thing I can use an NSString or is there something better and more effiecient? The reason I ask is because sometimes if the input is to big my program crashes.

Thanks a lot.

NSString (or better yet, NSMutableString) is probably your best option. It's extremely doubtful that 1 paragraph of text would take up too much memory and crash the app. Keep in mind that using ASCII encoding, 1 character is usually mapped to 1 bite in memory. This changes depending on the string encoding you're using (probably UTF8), but you would need at least hundreds of thousands of characters in your string to use even just 1 MB of RAM. Keep in mind also that the UITextView's text property is an NSString as well, and is using just as much memory as your string is.

What exception are you getting when the crash occurs?

If you're using UITextView for the text entry, you could set your string variable to the contents of your text view at predefined intervals, and in viewWillDisappear (Code: [myMutableString setString: myTextView.text] ). You could also use the delegate for UITextView to update your variable every time the user makes a change to the text view's text.

To easily persist the string, you can use [myString writeToFile:pathIWantToSaveTo] to save a PLIST. To set your string to the saved version, just use [myMutableString setString: [NSString stringWithContentsOfFile: pathISavedPLISTTo].

If that's not enough, you could also look into using Core Data or sqlite, but PLIST's should be fine for most apps that are only saving text (or simple NSDictionary's or NSArray's).
 

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
Oh that is great, thank you very much just what I needed. I do have another question though. Say I have a few different variables that are to be shared between multiple UIView's the way I have it set up right now is that I made an additional empty file ("Constant.h") and simply import that file into every view controller. Is that the best way to go about doing that?

The end result would be me eventually sending those variables in that constant.h file to a server.

Thanks again.
 

csnplt

macrumors 6502
Aug 29, 2008
320
1
Chicago Area
Oh that is great, thank you very much just what I needed. I do have another question though. Say I have a few different variables that are to be shared between multiple UIView's the way I have it set up right now is that I made an additional empty file ("Constant.h") and simply import that file into every view controller. Is that the best way to go about doing that?

The end result would be me eventually sending those variables in that constant.h file to a server.

Thanks again.

Do you intend to change the values of the variables in Constant.h, or are they just constants? Without knowing what your application is aiming for, I can't be sure what the best design is, but if you don't need to change the values of the variables, your idea should work well. Apple seems to do the same thing in some of their frameworks.

If you do intend to change the values of your variables, you should probably use a central database system of some type. It could be as simple as NSUserDefaults if that's all your app needs (to store a few settings) to a much more complex system like NSData or sqlite.

If you're sending the values in Constant.h to your server, it sounds to me like you intend to do something with them.

If you're comfortable sharing, what type of app are you writing?
 

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
No don't need to change them at all. The basic function of the application is to send a small amount of information to a server (a few strings maybe in integer ect). Then pull it back down from that server to a another iphone (one that calls upon that set of particular information). The server runs mySQL to keep everything organized....is that the best?

Thanks again so much.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.