0

I'm storing heights in my database as 62 or 511 instead of 6 ft 2 or 5 ft 11.

This is how I prepare the string that will be saved:

    let height = 6 ft 2
    let myNewString = height.stringByReplacingOccurrencesOfString(" ft ", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    println(myNewString) // 62

What I'd like to do is now do the opposite thing when displaying back the data from the database.

So I'd like to take the returned "62" from the database and insert this string " ft " into it. My aim is to end up with "6 ft 2" to display on the app.

How can I do this?

I want to avoid storing two columns dedicated to height in my database. I only know how to concatenate strings but this isn't exactly concatenating a string. I am inserting it into an existing string.

I'd appreciate an efficient way to do this. Been looking around for a while now and can't seem to find my solution.

Thanks for your time.

Update:

After reading comments I'm wondering if it would be better to add a decimal point after the first char in string e.g. 4.2, 4.10, 5.4, 5.11, 6.2 etc

let height = 6 ft 2
let myNewString = height.stringByReplacingOccurrencesOfString(" ft ", withString: ".", options: NSStringCompareOptions.LiteralSearch, range: nil)
println(myNewString) // 6.2
7
  • 2
    Your representation is ambiguous. 111 could be 11 ft 1 or 1 ft 11. Commented Apr 4, 2015 at 16:30
  • min age is 4 ft 0 and max is 7 ft 11 @MartinR Commented Apr 4, 2015 at 16:30
  • Ohh! I see what you're saying. I totally missed that. Back to the drawing board for me. Maybe I can add a decimal point after the first char Commented Apr 4, 2015 at 16:32
  • It's not ambiguous. Given the height range you stated, the first digit is always the feet and the remaining digits (if any) are the inches. Commented Apr 4, 2015 at 16:34
  • 3
    Rather than being more clever with format, just store it in inches. You'll even save space, if that's the goal, by using a 1-byte int rather than a three-byte string. Commented Apr 4, 2015 at 16:39

2 Answers 2

2

It would probably be a better idea to store the height purely in inches (e.g. store 74 for 6'2").

Anyway:

    let input = "62"
    let feet = (input as NSString).substringToIndex(1)
    let inches = (input as NSString).substringFromIndex(1)
    let output = "\(feet) ft \(inches)"

If you don't like the use of NSString, you can avoid it this way:

    let input = "62";
    let feet = input.substringToIndex(input.startIndex.successor())
    let inches = input.substringFromIndex(input.startIndex.successor())
    let output = "\(feet) ft \(inches)"
Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking of storing as 4'0" but the reason I didn't was because I was under the impression I wouldn't be able to use that format with parse query conditions e.g. query where height is greater than ..... and less than. I thought this would only work with integers only. But I'm in parse now and have manually edited some fields and clicked the column header and the fields have been put in correct order (lowest height to highest) so guessing it will work in code too. So I can just store as e.g. 6'2" and display this way also.
Are you sure that 5'10" will sort after 5'2"?
2

Swift’s String conforms to RangeReplaceableCollectionType, which has a splice method – splice allows you to insert a collection into another collection. Since strings are collections, that means you can write:

var height = "62"
if !height.isEmpty {
    height.splice(" ft ", atIndex: height.startIndex.successor())
} // height is now "6 ft 2"

Alternatively if you know you only want the first character before the “ft” you can do:

let height = "62"
if let feet = first(height) {
    let inches = dropFirst(height)
    let heightWithFt = "\(feet) ft \(inches)"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.