gpt4 book ai didi

swift - String.Index 没有名为 advanceBy 的成员

转载 作者:行者123 更新时间:2023-11-30 13:39:29 24 4
gpt4 key购买 nike

这是我在类里面作为练习的快速代码,我正在重写它(因为我是新手)。

位于let end = starIndex.advancedBy(position),我收到错误消息 String.Index 没有名为 advancedBy 的成员 我是还不确定这意味着什么或如何纠正它,非常感谢您帮助理解我做错了什么。

//: Playground - noun: a place where people can play

import UIKit

var str : String = "Hello, playground"

str

let strFix = " Can not change"
str = "Good bye"
//strFix = "Testing"

var str2 = str + "F"

var townName = "NeverLand"

let population = 30000

let numOfStopLight : Int = 10

print("\(townName) has populations of \(population)
and has \ (numOfStopLight) Stop Lights")

for c in townName.unicodeScalars
{
print ("\(c)")
}

let starIndex = str.startIndex

let position = 3

let end = starIndex.advancedBy(position)

let charAt = str[end]

let range = starIndex...end

str[range]

let aChar: Character = "\u{1F60E}"

str.append(aChar)

let dollarSign = "\u{24}" //$, Unicode scalar U+0024
let blackHeart = "\u{2665}" // , Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // , Unicod scalar U=1F496
str.append(Character(sparklingHeart))

for c in str.characters
{
print("\(c)")

}
for c in str.unicodeScalars
{
print("\(c.value)")
}

/////////////////////


struct Car
{
var make : String = ""
var price : Double = 0.0
let color : String = "RED"

func getMske()->String
{
return make
}
mutating func setMake (m: String)
{
make = m
}
func showCar()->String
{
return "Make:\(make) Price=\(price) Color= \(color)"

}
}
//let us make some Car

var myCar = Car(make: "Nissan", price: 45000.0)

print(myCar.showCar())

myCar.setMake("Nissan2016")

print (myCar.showCar())

//show pass value

func doChange (var c: Car)
{
c.setMake("Toyota")

print (c.showCar())


}

doChange(myCar)
print (myCar.showCar())


//let us have a class

class Xcar
{
var make :String = ""
var price : Double = 0.0
var color : String = ""

init(m :String, p: Double, c: String)
{
self.make = m
self.price = p
self.color = c
}
func setMake(m: String)
{
self.make = m

}
func showXCar()->String
{

return "Make: \(make) Price=\(price) Color=\(color)"
}
}
func doChangex( c:Xcar)
{
c.setMake("BMW")
print("\(c.showXCar())")

}
var hisCar = Xcar(m: "Fiat", p: 15000.0, c: "Blue")
print ("\(hisCar.showXCar())")

doChangex(hisCar)
//notice the function did change
//because it was a class
print ("\(hisCar.showXCar())")
//What is optional

let a: Float?
let b: Float?
let c :Float?

a = 10
b = 20
c = 5

//this is implicit unrap
let ave = (a! + b! + c!)/3.0

if let x = a, y = b, z = c
{
print ("\((x + y + z) / 3.0)")

}
else
{
print ("missing value...")



}


// note Array, Double, Float, Int and Dictionary are all struct

var intArray = [Int] ()
intArray.append(50)
intArray.append(100)
intArray.append(600)
intArray.maxElement()
intArray.capacity
intArray.count
intArray.description
intArray.dropFirst()
intArray.first
intArray.description
intArray.removeFirst()
intArray.description

for c in intArray
{
print("\(c)")

}


var dict = [Int :String ] ()

dict = [235 :"Jack", 100: "Joe", 60: "Lisa"]
dict.description

for(key, value) in dict
{
print ("\(key)")

}

var name = dict[235]

最佳答案

尝试在一行中完成

let startIndex = str.startIndex.advancedBy(3)

关于swift - String.Index 没有名为 advanceBy 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com