-4

ScreenShot of issue

I want to change the color the selected String from the cell. some chunks of code given below to more help to me.

'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion

{
    let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)as! FirstTableViewCell
    Cell.selectionStyle = .none

    Cell.marqueeLabel1.type = .continuous
    Cell.marqueeLabel1.animationCurve = .easeInOut

    let strings = "Purchase bids- 0123456789     Sell Bids- 0123456789     Cleared- 0123456789     "

    let txtfield1 :UITextField!

    let string_to_color = "0123456789"

    let range = (strings as NSString).range(of: string_to_color)

    let attribute = NSMutableAttributedString.init(string: strings)
    attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)

    txtfield1 = UITextField.init(frame:CGRect(x:10 , y:20 ,width:100 , height:100))
    txtfield1.attributedText = attribute

    Cell.marqueeLabel1.text = strings[Int(arc4random_uniform(UInt32(strings.count)))]
    Cell.marqueeLabel2.type = .continuous
    Cell.marqueeLabel2.animationCurve = .easeInOut
    let strings2 = ["Purchase bids- 0123456789     Sell Bids- 0123456789     Cleared- 0123456789     "]

    // Cell.marqueeLabel2.text = strings[Int(arc4random_uniform(UInt32(strings2.count)))]

    return Cell
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
Ashish Giri
  • 21
  • 1
  • 5

2 Answers2

0

try! unlike your string array like,

let array = ["Frodo", "sam", "wise", "gamgee"]
 Cell.marqueeLabel1.text = array [Int(arc4random_uniform(UInt32(array.count)))]
Community
  • 1
  • 1
john afordis
  • 317
  • 3
  • 15
0

This is working (you need to use the subscript methods as in doc: https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html#ID494):

let strings = " 1234 1234 4567"
let myString = strings[strings.index(strings.startIndex, offsetBy: String.IndexDistance(arc4random_uniform(UInt32(strings.characters.count))))]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ocunidee
  • 1,769
  • 18
  • 20
  • is it telling you that it cannot convert value of type Character to type String? By the way,why are you even trying to get a random character from strings? what's the purpose in what you try to do? – Ocunidee Jan 30 '17 at 11:40
  • I am not trying to get any random character I just want to change the color of the selected string from a string array of a marquee Label – Ashish Giri Jan 30 '17 at 12:00
  • you do not have a string array but one long string. What you are doing right now (and what is giving you the warning) is taking a random character within that long string (your variable strings) and assigning it to the text of the cell. Is it really something you need to do? – Ocunidee Jan 30 '17 at 12:33
  • here in the code just suggest me to change the color of a string "0123456789" from that one long string . as i am using a third party of a marquee label; – Ashish Giri Jan 30 '17 at 13:07