UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Im unable to understand very well day 29, working with strings. Can anyone spare me an explanation?

Forums > 100 Days of SwiftUI

I was doing day 29 and all was well till i reached "Working with strings". Im unable to understand how to use the methods explained in the chapter(every time i try to trim and output the result in a text view there are some errors) and also the part that they are from uikit but initially from obj-c(what does that even mean?).

1      

UIKit is the older framework for building apps in the Swift programming language that existed before SwiftUI. If you follow the "100 days of Swift" course on this website instead of the "100 days of SwiftUI" course, you will be learning how to use the UIKit framework.

Objective-C is the older programming language that has been used for development on apple products before the Swift programming language even existed.

I'm not sure what type of error you're getting when you try to trim the characters. Is your app not building? Or does the app crash when you try to type new words into it? It's hard to say without seeing what you are working with.

You can share segments of your code in these forums by wrapping your lines of code in triple back-ticks. (Press the key under the escape key on your keyboard 3 times. Move to a new line, then press that same key 3 times again. Then you can paste your code in lines between the two sets of triple back-ticks)

2      

@Fly0strich thanks for the explanation! this is my testing code:

struct ContentView: View {
    static let people = ["Finn", "Leia", "Luke", "Rey"]
    static var words = "Hello hell hi how hooo"
    static var word = words.trimmingCharacters(in: .whitespacesAndNewlines)

    var body: some View {
        List {
            Text("Hi: \(word)")

            ForEach(ContentView.people, id: \.self) {
                Text($0)
            }
            Text("Static Row")
        }
    }
}

and this:

struct ContentView: View {
    static let people = ["Finn", "Leia", "Luke", "Rey"]
    static var words = "Hello hell hi how hooo"
    static var word = words.trimmingCharacters(in: .whitespacesAndNewlines)

    var body: some View {
        List {
            Text("Hi: \(ContentView.word)")

            ForEach(ContentView.people, id: \.self) {
                Text($0)
            }
            Text("Static Row")
        }
    }
}

doesnt do anything.

1      

Ok, a few things here. I don't think you want to be declaring static variables here. So, just remove the static from your variables. Then, you don't need to write ContentView.people in your body. You can just write people instead.

However, you will need to make word a computed property, rather than trying to initialize it with (set it equal to) a value.

So, you will end up with something like this...

struct ContentView: View {
    let people = ["Finn", "Leia", "Luke", "Rey"]
    var words = "Hello hell hi how hooo"
    var word: String {
        words.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    var body: some View {
        List {
            Text("Hi: \(word)")

            ForEach(people, id: \.self) {
                Text($0)
            }
            Text("Static Row")
        }
    }
}

Another problem is that I don't think that .trimmingCharacters(in:) is doing what you think it is doing. That will remove any white spaces or new lines from both ends of your string, which will leave you with the same string you currently have in words. But, your app will build and run at this point.

1      

Now, I think I can see what you are trying to make this app do, and there are a few problems with that as well. I assume that you are wanting your list to show a random word from words along with a name from your people array on each line.

If that is the case, you will need to use String.components(separatedBy:) rather than String.trimmingCharacters(in:) to accomplish this. However, .components(separatedBy:) will return an array of strings, rather than a single string. So you will need to change the type of your computed property to reflect that. (I also suggest changing the name from word to something like wordArray)

Then, you will want to make sure that both of your text options (the name and the word) are included inside of the ForEach loop.

I used Int.random(in:) to make sure that a random word was selected from wordsArray on each item in the list, and ended up with this...

struct ContentView: View {
    let people = ["Finn", "Leia", "Luke", "Rey"]
    var words = "Hello hell hi how hooo"
    var wordArray: [String] {
        words.components(separatedBy: " ")
    }

    var body: some View {
        List {
            ForEach(people, id: \.self) {
                Text("\(wordArray[Int.random(in: 0...4)]) \($0)")
            }
        }
    }
}

1      

Thanks for the reply! Your explanation was awesome. but about the thing i was trying to do: i wanted to remove all the whitespace from the words string so that i get a string without any spaces. Thanks again.

1      

In that case, one way of doing it is to separate the string into an array of words as I described above, but then rejoin them into a single string.

    var word: String {
        words.components(separatedBy: " ").joined()
    }

That will remove the spaces, but there may be a more direct way to do it. I'm not sure.

1      

nice, thanks.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.