iOS

2017-12-02  本文已影响0人  映雪复习手册

1、Xcode


2、 iOS technology layers(要打印)


3、Objective-C HelloWorld(要打印)

image.png
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *lable1;

- (IBAction)clickBtn:(id)sender;

@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize lable1;

// Do any additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)clickBtn:(id)sender {
    lable1.text = @"Hello hyx";
}

@end

4、Swift HelloWorld(要打印)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    
    @IBAction func btnClick(sender: AnyObject) {
        label1.text = "Hello hyx!"
    }
    
    // Do any additional setup after loading the view, typically from a nib.
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

5、语法:Objective-C 、Swift 的区别

5.1 变量声明

Swift 可以不指定变量类型,var 就代表无类型,下面的四种定义方式都可以:

var width = 5;
var width: Int = 5
var width = Int(5)
var width = Int(5)

Primitive 类型:Int、Float、Double、Character、String、BOOL
Constants:Constants are a way of telling Swift that a particular value should not and will not change,(eg:let width = 5,let A: Int = 10)Swift’s compiler can optimize memory used for constants to make code perform better.

5.2 for 循环和 while 循环

for (int i = 0; i < 100; i++) {
    // Do something
}

int count = 0 ;
while (count < 10) {
    // Do something
    ++count ;
}

int count = 0 ;
do {
    // Do something
    ++count ;
} while (count < 10);
// 代表 i 的值从1 循环到100 
for i in 1 … 100 {
    // Do Something
}

while count < 10 {
    // Do Something
    count++
}

// 和  do while 一个意思
var count = 0
repeat {
    // Do Something
    ++count
} while count < 10

5.3 类和方法(要打印,C++的不打印)

.h 文件包含了 class 中属性和方法的定义:The header file that contains the declaration of a
class,Also contains declaration of member variables and methods of the class.

.m 文件包含了 class 中方法的实现:Contains the definition (implementation) of the
methods of the corresponding class, manipulation of data and objects.

People.h

// 引入系统的基础库
#import <Foundation/Foundation.h>
#import “Constants.h”

// 一定要继承 NSObject
@interface People: NSObject {
    NSString* name;
    int age;
}

// The “@property” lines are needed to get and 
// set the variables of an object of People class from other objects
//(加了@property才允许在外部访问这些变量)
@property NSString* name;
@property int age;

// 定义构造函数,People*是返回值
- (People*) initialize: (NSString*) name: (int) age ;
// 定义run函数,void是返回值,表示没有返回值
- (void) run: (float) distance;

@end

People.m

#import “People.h”

// 表示接下来是People类两个方法的实现
@implementation People

// This line directly corresponds to the two @property statements in the .h file.
// If you want to use the property to store a value and if you don’t use = here
// @synthesize uses the name of the property for storage
@synthesize name, age;

- (People*) initialize: (NSString*) name: (int) age {
    [self init];
    self.name= name;
    self.age = age ;
    return self;
}

- (void) run: (float) distance {
    // run run ......
}

// 这个必须写
- (void) dealloc {
    [super dealloc];
}

@end

Use Class People

// 只要是创建新对象,都需要写alloc,给新对象分配内存
People* hyx= [[People alloc] initialize: @"Huang YingXue": 22];
[hyx run: 666.66];

People* mxm= [[People alloc] initialize: @"Mo Xumin": 24];
[mxm run: 1000.66];

跟 Objective-C 一样,.h是类和方法的声明,.cc是类和方法的实现,下面看用法(Student继承了People):

Student.h

// 假设Student 继承 People,C++里在class声明处的冒号表示继承
class Student : People {

private:
  string name;
  int age;

public:
  Student(string name, int age);
  void run(float distance);
};

Student.cc(或Student.cpp)

include "Student.h"

Student::Student(string name, int age) {
  this->name = name;
  this->age = age;
}

void Student::run(float distance) {
    // run run
}

Use Class Student

Student *hyx = new Student("Huang YingXue",22);
hyx->run(666.666);
import Foundation

class People {

    var name: NSString
    var age: Int

    // 构造方法
    init(name: NSString, age: Int) {
        self.name = name
        self.age = age
    }

    // run 方法
    func run(distance:Float) {
        // run run ......
    }
}

Use People

var hyx = People(name:"Huang YingXue", age:22)
var mxm = People(name:"Mo Xumin", age:24)
hyx.run(distance:666.666)

5.4 类方法和实例方法(Class method and Instance method)

这个和 Java 的 static 类似,Java 的 static 方法是类方法,直接用类名调用方法People.run(),没加 static 的方法是实例方法,需要通过对象来调用hyx.run()

Objective-C里没有static关键字,通过+和-来区分,函数开头用-表示是实例方法,调用方式是[hyx run:6.6],函数开头用+表示是类方法,调用方式是[People eat: "吃屎"]

// 类方法,加class前缀
class func aClassMethod() {
    print("I am a class method") 
}

// 实例方法
func anInstanceMethod() {
    print("I am a instance method")
}

// 调用
People.aClassMethod();
hyx.anInstanceMethod();

5.5 Swift Function 的特殊定义形式

// 定义
func someFunction(a: Int, b: Int) {
 
}

// 调用
someFunction(a: 1, b: 2)
// 表示返回值是一个 String 字符串
func greet(person: String, from hometown: String) -> String  {
    return "Hello \(person)! Glad you could visit from \(hometown)."
}

// 调用
String result = greet(person: "Bill", from: "Cupertino");
// 定义
func someFunction(__ a: Int, __ b: Int) {
 
}

// 调用
someFunction(1, 2)
// 定义,b : Int 后加了个 = 2,表示如果调用的时候不写第二个参数,默认就当你传了2进去
func someFunction(a: Int, b: Int = 2) {
 
}

// 调用,这样写其实等于写了 someFunction(a:1, b:2)
someFunction(a:1)
// Double 后加了 ... 省略号,表示参数可以传任意多个 double 数,最后把所有传入的 double 树遍历一次,加起来,然后返回
func add(_ numbers: Double...) -> Double { 
    var total: Double = 0
    for number in numbers { 
        total += number
    }
    return total; 
}

// 调用
add(1,2,3,4,5,6);

5.7 String

// There are two types of strings: NSString and NSMutableString:
// 1) A string of type NSString cannot be modified once it is created
// 2) A string of type NSMutableString can be modified

// 普通字符串
NSString * lionStr = @“Lion";

// 格式化字符串
int age = 22;
float gpa = 3.6;
NSString *name = "YingXue"
NSString * str = [NSString stringWithFormat:@ "%@ age is %d, %@ gpa is %f ", name, age, name, gpa];
// 将会得到字符串: "YingXue age is 22, YingXue gpa is 3.6"

// 查找子字符串
NSString *Str = @“hello world”;
NSRange *range = [Str rangeOfString:@“world”];
// 得到结果:range.location is 6, range.length is 5

OC里的NSLog要看下,就是打印字符串在日志,如下:

// 和上面的 stringWithFormat 规则一样,看懂就好了
NSLog(@ "%@ age is %d, %@ gpa is %f ", name, age, name, gpa);
// Swift 的 String 有三种定义方式
var S = “hi”
var S: String = “hi”
var S = String(“hi”)

// 查找子字符串与 Objective-C 类似
var S = “hello world”
var range = S.rangeOfString(“world”)

5.8 数组 Array(要打印)

// 1) 普通数组,类似 Java
int myArray[100]; 
myArray[0] = 555; 
myArray[1] = 666;

// 2) 对象数组(nil表示空元素,也可当做数组结束符)
NSArray *array = [[NSArray alloc] initWithObjects: @”red”, @”green”, @”blue”, nil];
for (int i = 0; i < [array count]; i ++) { 
    // 打印
    NSLog(@”array[%d] = %@”, i, [array objectAtIndex: I]);
}

// 输出结果如下:
array[0] = red
array[1] = white 
array[2] = blue

// 3) Mutables数组(动态数组),类似 Java 的 ArrayList,10表示数组的初始长度是10
NSMutableArray *peoples = [[NSMutableArray alloc] initWithCapacity:10];
People* hyx= [[People alloc] initialize: @"Huang YingXue": 22];
People* mxm= [[People alloc] initialize: @"Mo Xumin": 24];
// 把两个 People 对象塞到数组里
[peoples addObject:hyx];
[peoples addObject:mxm];

// 使用完了要释放数组元素的内存
[[peoples objectAtIndex:0] release];
[[peoples objectAtIndex:1] release];
// 然后把数组元素从数组里删除
[peoples removeObjectAtIndex:0];
[peoples removeObjectAtIndex:1];
// 最后释放数组自身的内存
[peoples release];
// 1)普通数组的三种定义方式
var myArray: Array<Int> = [555, 666]
var myArray: [Int] = [555, 666]
var myArray = [555, 666]
var somevalue = myArray[0] // 得到值555

// 2)Mutables 数组承载对象
var hyx = People(name:"Huang YingXue", age:22)
var mxm = People(name:"Mo Xumin", age:24)
// 第一种写法
var peoples:[People]=[hyx, mxm]
// 第二种写法
var peoples:[People]=[]
peoples.append(hyx)
peoples.append(mxm)
// 然后遍历peoples数组里的每个元素,打印name
for i in 0..< peoples.count {
    print(peoples[i].name)
}

5.10 @property (weak, nonatomic)(要打印)

5.9 C++/Objective-C/Swift 代码互译(要打印)

-- C/C++ Objective-C Swift
Framework inclusion #include <stdio.h> #import <Foundation/Foundation.h> Import Foundation
Header File inclusion #include "People.h" #import "People.h" 不写
Constant Definition #define COUNT 9 #define COUNT 9 let COUNT = 9
Header File/ Implementation File C: .h/.c C++: .h/.cc .h/.m .swift
Member Variables Declaration int age; int age; var age: Int
Class Method Declaration static void helloWorld(){} + (void) helloWorld{} class func helloWorld() {}
Instance Method Declaration int getAge(){} - (int) getAge{} func getAge() {}
Dynamic Memory Allocation and Pointer variable Assignment People * hyx=malloc(sizeof(People)); People * hyx = new People(); People * hyx = [People alloc]; var hyx = People()
Class Method Invoke People::run(); [People run]; People.run()
Instance Method Invoke hyx->run(); [hyx run]; hyx.run();
String Format "Hello" @"Hello" "Hello"
Release Object Memory delete hyx [hyx release] 不需要手动释放内存

5.11 Optionals in Swift(要打印)

(1)When a property can be there or not there
(2)When a method can return a value or nothing, like searching for a match in an array
(3)When a method can return either a result or get an error and return nothing
(4)Delegate properties (which don't always have to be set)
(5)For a large resource that might have to be released to reclaim memory

5.12 Pointer(指针)

只说这三个关键字(OC的内存回收算法是基于引用计数,也就是说,有一个引用指向某个对象的时候,这个对象的引用就会被加1)
alloc:在创建对象的时候调用,表示为这个对象分配内存
retain:只要有一个引用指向这个对象,引用数就加1
release:表示要释放对这个对象的引用,引用数-1
当引用数为0时,对象的内存会被系统回收

// 我这里为了快,用C++写OC的例子,原理一样的
// 这时候 alloc 会被调用
People hyx1 = new People();
// 这时候hyx2的引用也指向了hyx1原来指向的对象,所以有两个对象同时指向了黄映雪对象
// 这时候retain会被调用,引用数变成了2
People hyx2 = hyx1;
// 释放两个引用,引用数变为0,之前new的对象内存就会被回收
delete hyx1;
delete hyx2;

6、Navigation Control

6.1 创建 Navigation 的步骤(要打印)

(1)Click and choose:Editor -> Embed In -> Navigation Controller
(2)Dragging “Bar Button Item” to the title bar and be named “next”
(3)Create more new “pages” by dragging “View Controller”
(4)Connect the “next ”button with the new “page” (“control” + drag),choose the action segue “Show”
(5)The “back” buttons will be created automatically


7、Tab Control

image.png

8、UIImage & UIImageView

8.1 Adding Images 步骤(缩印就好了)

(1)Drag the images into the “Resources” folder
(2)在 ViewController.m 中的 viewDidLoad 方法中添加如下代码:

// 创建 UIImageView 对象,加载名字为puppy.jpeg的图片
UIImageView *myImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"puppy.jpeg"]];
// 把 UIImageView 添加到界面中
[self.view addSubview:myImage];

8.2 Positioning Images

// (x,y)= (150,200)
myImage.center = CGPointMake(150, 200);

8.3 Scaling Images

// (x,y) = (0,0),width=50,height=25
myImage.frame = CGRectMake(0, 0, 50, 25);
// 1)设置后,中心坐标是(0,0),width为50,height为25
myImage.center = CGPointMake(150, 200); 
myImage.frame = CGRectMake(0, 0, 50, 25); 

2)设置后,中心坐标是(150,200),width为50,height为25
myImage.frame = CGRectMake(0, 0, 50, 25); 
myImage.center = CGPointMake(150, 200);

9、Touching Events

9.1 Single Touch

 (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    // 获得触摸位置的坐标
    CGPoint *location = [touch locationInView:touch.view];
    // 每次触摸都修改图片的坐标
    image.center = location;
}
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // 再次调用touchesBegan,实现了拖动图片的效果
    [self touchesBegan:touches withEvent:event];
}
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}

9.2 Multiple Touches

 (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 获取所有手指的触摸点
    NSSet *touches = [event allTouches];
    for (UITouch *myTouch in touches) {
        // 获取每个触摸点的坐标
        CGPoint location = [myTouch locationInView:self];
    }
}

10、Putting Your iOS Apps onto a Real Device(要打印)

10.1 Each app that is compiled to run on a device must be “code-signed”

Unlike Android , Apple has strict security regarding who can distribute apps.
(1)Your app is built and signed by you or a trusted team member.
(2)Apps signed by you or your team run only on designated development
devices.
(3)Apps run only on the test devices you specify.
(4)Your app isn’t using app services you didn’t add to your app.
(5)Only you can upload builds of your app to iTunes Connect.
(6)If you choose to distribute outside of the store (Mac only), the app can’t be modified and distributed by someone else.

10.2 证书分类(Certificate Type)(要打印)

image.png

11、传感器(Motion sensor)


12、Xcode vs. Cocoa

13、APPStore Apps review policy(可以打印)

  1. Safety:the app doesn’t contain upsetting or offensive content, won’t damage their device, and isn’t likely to cause physical harm from its use.

  2. Performance:App Completeness、Beta Testing、Accurate Metadata、Hardware Compatibility

  3. Business:There are many ways to monetize your app on the App Store. If your business model isn’t obvious, make sure to explain in its metadata and App Review notes. If we can’t understand how your app works or your in-app purchases aren’t immediately obvious, it will delay your review and may trigger a rejection.

  4. Design:不允许抄袭、APP应该有实用价值、和APPStore上已有的APP重复功能的APP也有可能会审核不通过

  5. Legal:Apps must comply with all legal requirements in any location where you make them available (if you’re not sure, check with a lawyer).
    Such as:Privacy、Intellectual Property、国家或地区对于Gaming, Gambling, and Lotteries的法律法规


14、访问网络

// 获取用户名和密码,看题目到时候从哪获取就是从哪获取
NSString* username = nameInput.text;
NSString* pass = passInput.text;

// 判断用户名密码如果为空,就停止程序(isEqualToString就是判断两个字符串是不是相等,就可以判断账号密码是不是为"")
if([nameInput.text isEqualToString: @""]||[passInput.text isEqualToString:@""]){
    ...
    return;
}

// 把用户名密码信息写入网络请求,设置一些网络配置信息
NSString *post=[[NSString alloc]initWithFormat:@"uname=%@&pwd=%@",username,pass];
NSData *postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength=[NSString stringWithFormat:@"%d",[postData length]];
// 需要访问的URL
NSURL *url=[NSURL URLWithString:@"http://www.cs.hku.hk/~c7506/login.php"];
// 访问网络返回的结果存放在theRequest里
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [theRequest setHTTPBody:postData];
// 连接网络
NSURLConnection*theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];

// 下面是几个连接网络的方法,抄下来就好了
 -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
     [webData setLength:0];
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
     [webData appendData:data];
}
 -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
     [connection release];
     [webData release];
}

 // 访问网路结束后,会调用这个函数
 -(void)connectionDidFinishLoading:(NSURLConnection*)connection {
    NSString* loginStatus=[[NSString alloc]initWithBytes:[webData mutableBytes]length:[webData length]encoding:NSUTF8StringEncoding];
    NSLog(loginStatus);
    [connection release];
    [webData release];
}

15、其他

上一篇 下一篇

猜你喜欢

热点阅读