IOS - Concatenate multiple strings & vars [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm struggling with what I imagine is fairly simple - but I need to create a string by joining various strings and string vars together - this is what i have so far -
_msgTxt = #"I have achieved great results with my instructor%#", _usrName, #"Check her out here", _usrURL;
any tips on where i'm going wrong? I'm hoping to achieve a long string ie ' I have achieved great results with my instructor Zoe Edwards. Check her out here http://www.nme.com" which could be posted to social media channels.
Cheers

You'll need to use stringWithFormat.
Example:
_msgTxt = [NSString stringWithFormat:#"I have achieved great results with my instructor %#. Check her out here %#", _usrName, _usrURL];
One thing to keep in mind using the example above is that the objects/variables provided should appropriately use the description method to output user visible strings. NSString does, but other objects may output something which isn't user friendly.
If this is the case, you should use an NSString object within the parent object to display the information (You'd need to create this yourself; _usrURL.userFriendlyString for example).

Use [NSString stringWithFormat:*enter you stuff here*];

While stringWithFormat: will work as proposed by the other answers, it isn't very efficient if you just want to concatenate a number of strings in a set order. The power of stringWithFormat: comes from the contents of the format and the ability to reorganise and 'format' contents with the parameter specifiers. But it comes with a cost because the format string has to be parsed and processed.
For simple string concatenation, use NSMutableString and the appendString: method. (and note that you can also use appendFormat: of you have one part that needs it...).

Related

How to parse string to call array and its array contents [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to parse string and call array using valueForKeyPath:
ex:
NSArray *myArray = #[1,2,3,4,5,6,7,8];
I have a string's as below
myArray[], myArray[3],myArray[1..3]
when i evaluate these strings i want output like below
evaluate(#"myArray[]")
output:#[1,2,3,4,5,6,7,8]
evaluate(#"myArray[3]")
output:4
evaluate(#"myArray[1..3]")
output:#[2,3,4]
I am already using custom function using valueForKeyPath: for calling methods and functions. I am stuck with array and parameters
You have shown nothing you have attempted, so it is unclear what your problem is – you appear to know about things (valueForKeyPath:, regular expressions) you could build a solution from.
Here is an outline of a solution:
Your strings all appear to be of one of three forms: an array name followed by zero, one or two integers in square brackets. You can break this up to extract the one to three important parts using (at least) NSRegularExpression or NSScanner. If you pick NSRegularExpression just be careful writing the pattern as [, ], and . are special characters and will need appropriate escaping – just read the documentation.
Once you have your three parts you can obtain the value of the array using valueForKey: assuming it is either a property or an instance variable.
If you have matched/scanned one or two indexes then you can use the methods of NSArray to obtain the object(s), you might find NSIndexSet useful here.
That's it. If you try to do this and get stuck edit your question showing what you have tried, state what is broken, and ask for help; someone will probably be able to help further.
HTH

Make lowercase letters in a string uppercase [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Are there any methods for taking a string and converting all lowercase letters to uppercase?
I was thinking of making a for-loop to run through, check each character, see if it is in range 0061-007A (lowercase letters) and just subtracting 26 (base 16) (converts to the uppercase counterpart) from the unicode code and adding that character back to the string.
But I figured I'd check if there is a simpler method already out there... googled but couldn't find anything... I'm sure I could use a 1x1 UIWebView and load some javascript (that does this) with my string into the UIWebView but there has got to be something already in Objective-C other than the manual approach I first mentioned right?
You do not need a loop - you can use either
NSString *upper = [src uppercaseString];
or
NSString *upper = [src uppercaseStringWithLocale:myLocale];
for targeting a specific locale.

NSString's hash method and back? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Once I use [NSString hash] and get a NSUInteger, is there any way I can use that NSUInteger and turn it back into the original NSString? Apple doesn't really say anything about the implementation of the hash method in the docs.
FYI: I'm trying to store identifierForVendor as a NSNumber (specifically in either the major or minor property of a CLBeacon).
No. The hash is 32 or 64 bits, a string can be much longer, so it is inherently lossy, and the hash values are not unique (the same hash corresponds to multiple strings).
Actually, hash is not supposed to be de-coded. You may want to read something like http://en.wikipedia.org/wiki/Hash_function
Apple says "If two string objects are equal (as determined by the isEqualToString: method), they must have the same hash value". That's all you can get.
If you want to store it for later comparisons, then you should hash the both NSStrings & compare the resulting NSUIntegers

Ruby - converting a hashtag to actual word(s) ? (#contentmarketing => content marketing) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hashtags sometimes combine two or more words, such as:
content marketing => #contentmarketing
If I have a bunch of hashtags assigned to an article, and the word is in that article, i.e. content marketing. How can I take that hash tag, and detect the word(s) that make up the hashtag?
If the hashtag is a single word, it's trivial: simply look for that word in the article. But, what if the hash tag is two or more words? I could simply split the hashtag in all possible indices and check if the two words produced were in the article.
So for #contentmarketing, I'd check for the words:
c ontentmarketing
co ntentmarketing
con tentmarketing
...
content marketing <= THIS IS THE ANSWER!
...
However, this fails if there are three or more words in the hashtags, unless I split it recursively but that seems very inelegant.
Again, this is assuming the words in the hash tag are in the article.
You can use a regex with an optional space between each character to do this:
your_article =~ /#{hashtag.chars.to_a.join(' ?')}/
I can think of two possible solutions depending on the requirements for the hashtags:
Assuming hashtags must be made up of words and can't be non-words like "#abfgtest":
Do the test similar to your answer above but only test the first part of the string. If the test fails then add another character and try again until you have a word. Then repeat this process on the remaining string until you have found each word. So using your example it would first test:
- c
- co
- ...
- content <- Found a word, start over with rest
- m
- ma
- ...
- marketing <- Found a word, no more string so exit
If you can have garbage, then you will need to do the same thing as option 1. with an additional step. Whenever you reach the end of the string without finding a word, go back to the beginning + 1. Using the #abfgtest example, first you'd run the above function on "abfgtest", then "bfgtest", then "fgtest", etc.

A calculator app on iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone please explain the difference between this
[display setText:[[display text] stringByAppendingString:digit ]];
and this
[display setText:digit];
The code is rather clear. But if you don't understand:
Here [display setText:[[display text] stringByAppendingString:digit ]]; a new digit will be added to the digits currently displaying on the screen. This BOOL value userIsInTheMiddleOfTypingANumber is extremely straightforward - it is said that there are always digits on the screen and a new digit must be added to them. This method stringByAppendingString returns a new string made by appending a given digit to the currently displayed digits in the UITextField.
And here [display setText:digit]; all the text which are displayed in the UITextField will be overwritten with a new digit value. But as I suppose it is used when there are no digits on the screen and we need to write the first one.I don't know what is using for displaying digits in that app. But if it is UITextField then using setText is a bad idea - it is a deprecated method. You should use text property instead.
This is an extremely simple code which you need to understand yourself. So my advice you to read some introductory books on CocoaTouch and iOS with simple examples there are plenty of them: http://www.amazon.com/Beginning-iOS-Development-Exploring-SDK/dp/1430245123/ref=pd_sim_b_8 . And don't forget to use official documentation.

Resources