swift版本:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textFields1: UITextField!
@IBOutlet weak var textFields2: UITextField!
@IBOutlet weak var resultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func clickBtn(_ sender: UIButton) {
这种方法可行
// let str1 : NSInteger = (Int)(self.textFields1.text!)!
// let str2 : NSInteger = (Int)(self.textFields2.text!)!
第二种方法:
let str3 : NSInteger = (self.textFields1.text as NSString).integerValue
let str4 : NSInteger = (self.textFields2.text! as NSString).integerValue
//str3报错误: Cannot convert value of type 'String?' to type 'NSString' in coercion(强制)
self.resultLabel.text = String.init(format: "%zd", (str1 + str2)
)
}
oc版本:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textFiled2;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@end
@implementation ViewController
- (IBAction)btn:(id)sender {
NSLog(@"aaa");
NSString *str1 = self.textField1.text;
NSString *str2 = self.textFiled2.text;
NSInteger num1 = str1.integerValue;
NSInteger num2 = str2.integerValue;
NSInteger result = num1 + num2;
// self.resultLabel.text = @(result).description;
self.resultLabel.text = [NSString stringWithFormat:@"%zd",result];
}
爬坑1
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1,A 2004 Harris Poll reported that the majority of Americ...