New iphone code
You're looking for some new iPhone code!
Here are a few examples of interesting iPhone-related code snippets:
1. Swift Code:
- A simple "Hello, World!" app in Swift:
import UIKit
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50)) label.text = "Hello, World!" label.textAlignment =.center view.addSubview(label) } }
* A basic calculator app in Swift:
```swift
import UIKit
class CalculatorViewController: UIViewController {
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var numberPad: UIView!
var currentNumber: Double = 0
var previousNumber: Double = 0
var operation: String = ""
@IBAction func numberPressed(_ sender: UIButton) {
let number = sender.title(for:.normal)!
currentNumber = currentNumber * 10 + Double(number)!
displayLabel.text = String(currentNumber)
}
@IBAction func operationPressed(_ sender: UIButton) {
operation = sender.title(for:.normal)!
previousNumber = currentNumber
currentNumber = 0
displayLabel.text = "0"
}
@IBAction func equalsPressed(_ sender: UIButton) {
let result = calculateResult()
displayLabel.text = String(result)
currentNumber = result
previousNumber = 0
operation = ""
}
func calculateResult() -> Double {
switch operation {
case "+":
return previousNumber + currentNumber
case "-":
return previousNumber - currentNumber
case "*":
return previousNumber * currentNumber
case "/":
return previousNumber / currentNumber
default:
return 0
}
}
}
2. Objective-C Code:
- A simple "Hello, World!" app in Objective-C:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; label.text = @"Hello, World!"; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label]; }
@end
* A basic calculator app in Objective-C:
```objective-c
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *displayLabel;
@property (weak, nonatomic) IBOutlet UIView *numberPad;
@property (nonatomic) double currentNumber;
@property (nonatomic) double previousNumber;
@property (nonatomic, copy) NSString *operation;
- (IBAction)numberPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
- (IBAction)equalsPressed:(UIButton *)sender;
@end
@implementation CalculatorViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.currentNumber = 0;
self.previousNumber = 0;
self.operation = @"";
}
- (IBAction)numberPressed:(UIButton *)sender {
NSString *number = sender.titleLabel.text;
self.currentNumber = self.currentNumber * 10 + [number doubleValue];
self.displayLabel.text = [NSString stringWithFormat:@"%.2f", self.currentNumber];
}
- (IBAction)operationPressed:(UIButton *)sender {
self.operation = sender.titleLabel.text;
self.previousNumber = self.currentNumber;
self.currentNumber = 0;
self.displayLabel.text = @"0";
}
- (IBAction)equalsPressed:(UIButton *)sender {
double result = [self calculateResult];
self.displayLabel.text = [NSString stringWithFormat:@"%.2f", result];
self.currentNumber = result;
self.previousNumber = 0;
self.operation = @"";
}
- (double)calculateResult {
switch (self.operation) {
case @"+":
return self.previousNumber + self.currentNumber;
case @"-":
return self.previousNumber - self.currentNumber;
case @"*":
return self.previousNumber * self.currentNumber;
case @"/":
return self.previousNumber / self.currentNumber;
default:
return 0;
}
}
@end
These are just a few examples of iPhone-related code snippets. If you have a specific question or need help with a particular problem, feel free to ask!