所以,开始使用 Swift 和开发 iOS 应用程序。
做了一个将狗年龄转换为人类年龄的初学者教程。
这真的很基础,只是基于 5 的倍数的年龄,但我觉得不准确。 (因此所有丑陋的 if else 条件)
我有两个问题:
完成相同的年龄计算但最小化所有 if else 语句的最佳方法是什么?一定有更好的方法。
我用了 30 年的最后一个 if else 语句应该为 dogAge 变量分配一个字符串。我在将 var 从 int 转换为 string 时遇到了麻烦,并且一直在尝试我研究过的各种方法,但到目前为止,没有成功。
提前致谢!
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var dogAgeTextField: UITextField!
@IBOutlet weak var resultLabel: UILabel!
@IBAction func reset(sender: AnyObject) {
resultLabel.text = " "
dogAgeTextField.text=" "
}
@IBAction func findAge(sender: AnyObject) {
var dogAge = Int(dogAgeTextField.text!)!
if (dogAge == 1){dogAge=15}
else if (dogAge == 2) {dogAge=24}
else if (dogAge == 3) {dogAge=29}
else if (dogAge == 4) {dogAge=34}
else if (dogAge == 5) {dogAge=38}
else if (dogAge == 6) {dogAge=42}
else if (dogAge == 7) {dogAge=47}
else if (dogAge == 8) {dogAge=51}
else if (dogAge == 9) {dogAge=56}
else if (dogAge == 10) {dogAge=60}
else if (dogAge == 11) {dogAge=65}
else if (dogAge == 12) {dogAge=69}
else if (dogAge == 13) {dogAge=74}
else if (dogAge == 14) {dogAge=78}
else if (dogAge == 15) {dogAge=83}
else if (dogAge == 16) {dogAge=87}
else if (dogAge == 17) {dogAge=92}
else if (dogAge == 18) {dogAge=96}
else if (dogAge == 19) {dogAge=101}
else if (dogAge == 20) {dogAge=105}
else if (dogAge == 21) {dogAge=109}
else if (dogAge == 22) {dogAge=113}
else if (dogAge == 23) {dogAge=117}
else if (dogAge == 24) {dogAge=121}
else if (dogAge == 25) {dogAge=125}
else if (dogAge == 26) {dogAge=dogAge*5}
else if (dogAge == 27) {dogAge=dogAge*5}
else if (dogAge == 28) {dogAge=dogAge*5}
else if (dogAge == 29) {dogAge=dogAge*5}
else if (dogAge >= 30) {var dogAge=String(UTF8String: "really old")!}
resultLabel.text = "Your dog is \(dogAge) in human years."
}
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.
}
最佳答案
问题是您不能将 String 分配给声明为 Int 的变量
整个表达式可以简化为一个数组来映射狗年/人年。
let dogToHuman = [0, 15, 24, 29 ... 140, 145] // complete the array with the missing values
if let dogAge = Int(dogAgeTextField.text!) {
let humanAge : String
if dogAge >= 30 {
humanAge = "really old"
} else {
humanAge = String(dogToHuman[dogAge])
}
resultLabel.text = "Your dog is \(humanAge) in human years."
} else {
resultLabel.text = "The input is not an integer"
}
关于ios - 无法将类型 'string' 的值分配给类型 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604763/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun