How to avoid optional word from the app? - macos

I removed optional from assistance editor by unwrapping but how to get rid of it in the app and get only the number in answer and not the optional word with it?

Since there are two optionals(text from textfield, String to Integer typecasting) you can use the multiple if let conditions here
if let catText = catAge.text ,let cat = Int(catText) {
let age = cat * 7
catAge.text = "your cat is \(age) years old"
}

Related

How do I extract the prefix of a string till the last instance of a specific character?

for eampaple, the specific character is "_":
let str = "a_b_c_d_1";
how can I extract "a_b_c_d" ?
Found a way to do so, I believe there are more elegant ones.
let str = "a_b_c_d_1";
print(strcat_array(array_shift_right(split(str, "_"), 1),""))

How to remove first 3 characters in a string [duplicate]

This question already has answers here:
How Do I Use VBScript to Strip the First n Characters of a String?
(4 answers)
Closed 6 years ago.
I have a program that prints a string to a notepad file, the output being something random:
#'f7ruhigbergbn
I want to however remove the first 3 characters from the pasted result, how can I do this?
myString = "#'f"
result = workings - myString
This does not work, bare in mind the first 3 characters are always going to be #'f
Any thoughts? thanks
You can use:
result = Mid(workings, 4)
You can use Right function for get the X characters of the right side of string. With Len function you can get the length of the string.
Right(myString,Len(myAtring) - 3)
With this, you get a new string whitout the three first characters, now you can assign to the same string:
myString = Right(myString,Len(myAtring) - 3)
Try this:
mystring = "#'f7ruhigbergbn"
result = Mid(mystring, 3, mystring.length)

Ruby - Split a String to retrieve a number and a measurement/weight and then convert numberFo [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 need to split a string, for food products, such as "Chocolate Biscuits 200g"
I need to extract the "200g" from the String and then split this by number and then by the measurement/weight.
So I need the "200" and "g" separately.
I have written a Ruby regex to find the "200g" in the String (sometimes there may be space between the number and measurement so I have included an optional whitespace between them):
([0-9]*[?:\s ]?[a-zA-Z]+)
And I think it works. But now that I have the result ("200g") that it matched from the entire String, I need to split this by number and measurement.
I wrote two regexes to split these:
([0-9]+)
to split by number and
([a-zA-Z]+)
to split by letters.
But the .split method is not working with these.
I get the following error:
undefined method 'split' for #MatchData "200"
Of course I will need to convert the 200 to a number instead of a String.
Any help is greatly appreciated,
Thank you!
UPDATE:
I have tested the 3 regexes on http://www.rubular.com/.
My issue seems to be around splitting up the result from the first regex into number and measurement.
One way among many is to use String#scan with a regex. See the last sentence of the doc concerning the treatment of capture groups.
str = "Chocolate Biscuits 200g"
r = /
(\d+) # match one or more digits in capture group 1
([[:alpha:]]+) # match one or more alphabetic characters in capture group 2
/x # free-spacing regex definition mode
number, weight = str.scan(r).flatten
#=> ["200", "g"]
number = number.to_i
#=> 200
I'm not an expert in ruby, but I guess that the following code does the deal
myString = String("Chocolate Biscuits 200g");
weight = 0;
unit = String('');
stringArray = myString.split(/(?:([a-zA-Z]+)|([0-9]+))/);
stringArray.each{
|val|
if val =~ /\A[0-9]+\Z/
weight = val.to_i;
elsif weight > 0 and val.length > 0
unit = val;
end
}
p weight;
p unit;

removeRange() function gives empty string in Swift 2

The removeRange: is giving me an empty string in Swift 2 and I don't understand why.
The example in the apple documentation is:
var welcome = "hello!"
let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex
welcome.removeRange(range)
//I get "" as result rather than "hello" where the exclamation mark is removed
What could be the problem?
You start at the endIndex and then you go back by 6. You are now at the beginning of the word. Then you make a range to the end index and you remove the content of this range: of course there's nothing left. :)
For example, it could be this instead:
var welcome = "hello!"
let range = welcome.endIndex.advancedBy(-1)..<welcome.endIndex
welcome.removeRange(range)
Or this:
var welcome = "hello!"
let range = welcome.startIndex.advancedBy(5)..<welcome.endIndex
welcome.removeRange(range)
There's many possible combinations.
the string in apple documentation is
at the time of remove range
var welcome = "hello there!"
The value of welcome.endIndex is 6 so advancedBy(-6) means it goes to 0. Then the range = 0..<6 that means the the range cover the whole string.
If you want "hello" then change only advancedBy(-1).
var welcome = "hello!"
let range = welcome.endIndex.advancedBy(-1)..<welcome.endIndex
welcome.removeRange(range)

Manipulating strings with Swift

I am trying to divide a String in Swift. I have the following string
Program - /path/to/file.doc
I want to get three informations out of this string
Program
/path/to/file.doc
file.doc
I began with the following solution
var str = "Program - /path/to/file.doc"
let indi = str.rangeOfString("-")?.startIndex
let subString = str.substringWithRange(Range<String.Index>(start: str.startIndex, end: indi!))
let subString2 = str.substringWithRange(Range<String.Index>(start: indi!, end: str.endIndex))
This gives me the results
"Program "and
"- /path/to/file.doc"
But how can I get file.doc after the last /?
How Can i increase/decrease and range index to avoid blank spaces?
Yes, sidyll's suggestion is correct, it's a very common practice to get components of Unix path by converting it to NSURL. You may want to write something like this:
var str = "Program - /path/to/file.doc"
if let indi = str.rangeOfString(" - ")?.startIndex {
let subString = str.substringWithRange(Range<String.Index>(start: str.startIndex, end: indi))
let subString2 = str.substringWithRange(Range<String.Index>(start: indi, end: str.endIndex))
let fileName = NSURL(string: subString2).lastPathComponent()
}
I strongly suggest you don't do force unwrap like this. Consider situation if this code will work with string without a particular pattern, for example empty string. Correct, runtime error.

Resources