1

I know the basic way of replacing an item in a string, but now how I am doing it. Here's my code. I'm trying to search a note for certain words and swap them out when I do find them. I have certain words to replace other certain words with and I need to match them up so the sentence still makes sense.

func searchNote1()
{
    let contentArr = note1.content.componentsSeparatedByString(" ")
    let count = contentArr.count
    var num = 0
    while num < count
    {
        if conciseArr.contains(contentArr[num])
        {
            contentArr[num] = conciseArr []
        }
        num += 1
    }
}

I am trying to replace the string in contentArr[num] (wherever a certain word is found) and swap it out with the string found in conciseArr. Where I am stuck is how do I know the location of the word in conciseArr to replace the other word with?

3 Answers 3

2

Rather than .contains, look at indexOfObject. If the object exists in the conciseArray, you will get the index (location), otherwise you will get an NSNotFound. Then, if (location != NSNotFound)... (do your swap using the known indices)

And, while we're at it, perhaps fast enumeration would be appropriate here? (I'm an Obj-C guy; I hope my Swift syntax is right!)

func searchNote1()
{
    let contentArr = note1.content.componentsSeparatedByString(" ")
    for (index, element) in contentArr.enumerate()
    {
        var location = conciseArr.indexOfObject(element)
        if (location != NSNotFound)
        {
            contentArr[index] = conciseArr[location]
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This worked, temporarily. Now when the function has to replace more than one word in a sentence it just becomes one big jumble. Any idea on how to fix this?
I would need to see the input and output to understand what is happening.
Is there a way to PM on here or something? I don't want to post another question if I don't have to.
@SethRodgers I don't think there is a PM. You don't need to post another question, just put in some sample data and your result here, and I'll fix my answer to address your problem.
1

Given a sentence and a dictionary of synonyms

let sentence = "What a wonderful world"
let synonyms = ["wonderful":"beautiful", "world":"planet"]

This is how you get a new sentence with the synonyms

let newSentence = sentence
    .componentsSeparatedByString(" ")
    .map { synonyms[$0] ?? $0 }
    .joinWithSeparator(" ")

That's it

print(newSentence) // What a beautiful planet

Comments

0

This should help you out:

if conciseArr.contains(contentArr[num])
        {
            contentArr[num] = conciseArr.indexOf(contentArr[num])
        }

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.