Cannot convert value of type ‘Int’ to expected argument type ‘AttributedString.Index’

Sorry I am still a bit new to swift. I have searched on here but all I could find are outdated answers. How do I construct the AttributedString.Index for insert(_ s: some AttributedStringProtocol, at index: AttributedString.Index).

I know I can just add them together but this is just a small piece of what I am actually working on.

    func setFooterMessage() -> AttributedString {
        var mOne = AttributedString("Play   Monday")
        mOne.foregroundColor = .white
        
        let mTwo = AttributedString("on")
        mOne.insert(mTwo, at: 6) // error
        
        return mOne
    }

I’m not entirely sure where the 6 comes from or why there are 3 spaces in "Play Monday", as they cause a rather oddly asymmetric output, but the initialisers you need are as follows:

func setFooterMessage() -> AttributedString {
    var s: String = "Play   Monday"
    var mOne = AttributedString(s)
    mOne.foregroundColor = .white
        
    let mTwo = AttributedString("on")
    // *** You need to get the base index from the `String`
    let i = String.Index(utf16Offset: 6, in: s)
    // *** and from that you can get the index into the `AttributedString`
    if let ia = AttributedString.Index(i, within: mOne) {
        // ... but it's optional, as obviously it could fail
        mOne.insert(mTwo, at: ia) // no error now
    }
    return mOne
}

The rationale behind String.Index as opposed to Int has been explained in hundreds of answers here and elsewhere, but the overly simplified answer is that in Swift Strings are Unicode sequences, not byte arrays.

AttributedStrings are even more complex in that they have embedded attributes (colours, fonts or even entire images) and so to refer in any meaningful sense to a position in the AttributedString‘s character sequence, you must do so with reference to the equivalent String.

String and AttributedString don’t work with Int indices because characters can have more than one byte.

In case of AttributedString you can use the API index(_:offsetByCharacters:) which calculates the proper index

func setFooterMessage() -> AttributedString {
    var mOne = AttributedString("Play  Monday")
    mOne.foregroundColor = .white
    
    let mTwo = AttributedString("on")
    let index = mOne.index(mOne.startIndex, offsetByCharacters: 6) // actually 5
    mOne.insert(mTwo, at: index)
    
    return mOne
}

Another way is to get the index by the range of “Play ” and insert the substring at its upperbound

func setFooterMessage() -> AttributedString {
    var mOne = AttributedString("Play  Monday")
    mOne.foregroundColor = .white
    
    let mTwo = AttributedString("on")
    let index = mOne.range(of: "Play ")!
    mOne.insert(mTwo, at: index.upperBound)
    
    return mOne
}

Related: How to remove a substring from string of particular index to some length in swift

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật