gpt4 book ai didi

arrays - Swift 计算 for 循环中的字符串大小

转载 作者:可可西里 更新时间:2023-11-01 01:03:56 24 4
gpt4 key购买 nike

对于我的家庭作业,我必须以 3 种不同的方式转换用户输入的句子。

  • 将所有字母转换为大写(已完成)
  • 将所有字母转换为小写(已完成)
  • 将大写字母转换为小写字母,反之亦然(有问题)

我相信我的逻辑是正确的,但我不知道我哪里错了。

这是我的代码:

//
// ViewController.swift
// Lower Upper Converter
//
// Created by Mac User on 9/27/15.
// Copyright © 2015 Omid Nassir. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

private var input = ""
private var i = 0
var sentense = [String]()
//var sentenseChar: [Character] = []

//input fields
@IBOutlet weak var input1: UITextField!

@IBOutlet weak var input2: UITextField!




//caps button
@IBAction func capsBtn(sender: AnyObject)
{
if input1.text != nil
{
input = input1.text!.uppercaseString
input2.text = input
}
else
{
input2.text = ""
}
}

//lower case button
@IBAction func lowsBtn(sender: AnyObject)
{
if input1.text != nil
{
input = input1.text!.lowercaseString
input2.text = input
}
else
{
input2.text = ""
}
}


//word converter button
@IBAction func capsLowsBtn(sender: AnyObject)
{

if input1.text != nil
{

for i=0; i < count(input1.text); i++
{
input = advance(input1.startIndex, i)
str[input]
sentense[i] = advance(input.startIndex, i)
}

}

}//end of convert function


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

我收到一条错误消息:

 Cannot invoke 'count' with an argument list of type '(String?)'

对于此语句:for i=0; i < count(input1.text); i++

最佳答案

注意!对于 Swift 2,您必须使用新的语法:

将您的代码更改为:

for var i = 0; i < input1.text!.characters.count; i++
{
...
}

关于arrays - Swift 计算 for 循环中的字符串大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32853956/

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