Code Monkey home page Code Monkey logo

stoptest's Introduction

StopTest紀錄手冊

這份手冊主要紀錄Swift撰寫 app -> Server PHP -> DB 技術過程及重點。

使用技術部份(未編輯)

以Post方式傳送資料給網站(Post to Server)

1.首先須建立好一個Server,本專案是用php來開發 2.http request使用的類別為NSMutableURLRequest,他是繼承NSURLRequest類別

程式段落:

  var post:NSString = "CITY_NO=\(USERNAME_S)&CITY_NAME=\(PASSWD_S)"
  NSLog("PostData: %@", post);
        
  var url: NSURL = NSURL(string: "http://localhost:8888/parking.php")!
  var postData:NSData = post.dataUsingEncoding(NSUTF8StringEncoding)!
  var postLength:NSString = String( postData.length )
  var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
        
  request.HTTPMethod = "POST"
  request.HTTPBody = postData
  request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
  request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
  request.setValue("application/json", forHTTPHeaderField: "Accept")
        
  var reponseError: NSError?
  var bodyBata = "data=something"
  var response: NSURLResponse? = nil
  var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
        
  if ( urlData != nil ) {
    let res = response as! NSHTTPURLResponse!
    NSLog("Response code: %ld", res.statusCode);
            
    if (res.statusCode >= 200 && res.statusCode < 300){
      var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!
      NSLog("Response ==> %@", responseData);
                
      var parkData = parkingData()
      parkingList = parkData.getParkList()
    }
    return "01"
  }else{
    println("Cannot connect!")
    return "02"
  }

OC和swift差別 (OC & Swift)

NSArray和NSDictionary是Objective-C描述Array和Dictionary用 所以看到這兩個就可以直接想成swift的 Array/Dictionary<String,AnyObject>

改一下 Array/Dictionary<NSObject,AnyObject> 語法密糖 就可以寫成 [AnyObject]/NSObject:AnyObject 因此就能了解從[AnyObject],他是描述這個Array可以放AnyObject 也就是任意型別 Array是完整寫法,[AnyObject]是簡寫,但兩個都會做出一樣的東西

閉包 (closure)

閉包是自包含的函式程式碼區塊,可以在程式碼中被傳遞和使用。 Swift 中的閉包與 C 和 Objective-C 中的程式碼區塊(blocks)以及其他一些程式語言中的 lambdas 函式比較相似。

1.基本架構closure { (參數) -> 回傳值 in 程式實作 }

2.作為非同步方式,請外部傳一個closure,讓你的程式在完成的時候可以告知他

3.非同步觀念,如果你還要等他完成 那跟同步有什麼不同? (有些library在一些function會有一個completion的傳入參數 如果是Facebook 比較愛用completionHandler)

以下兩個例子為,請外部傳一個closure,讓你的程式在完成的時候可以告知他

//請外部傳一個closure,讓你的程式在完成的時候可以告知他
//這個例子是沒有東西的
func getData(completion:()->Void){
    /*
       執行非同步
    */
    dispatch_async(...){
    	/* Block A */
    	//完成的時候,執行completion這個closure
    	completion()
    }
}
//假設你會產生一些東西(e.g.)server傳下來的東西
func getData(completion:(parkings:[AnyObject])->Void){
    /*
       執行非同步
    */
    dispatch_async(...){
    	/* Block A */
        let data = /* data from server */
    	//完成的時候,執行completion這個closure
    	completion(parkings: data)
    }
}

//例子1 在使用的時候就會變成
getData( { ()->Void in
  //do something after callback
})

//例子2 在使用的時候就會變成
getData( { (parkings:[AnyObject])->Void in
  //do something after callback
  for parking in parkings {
    //do something
  }
     
})

stoptest's People

Contributors

ghostwolf90 avatar

Stargazers

Roman avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

stoptest's Issues

嗨!我们在iOS开发VPN的过程中遇到一些问题,想请教您

https://github.com/freeblow/Mango-Xary.git
我们是基于Mango这个项目进行开发的,正常的配置完成连接是可以连接成功的
但是因为一些需求,需要进行路由配置,添加规则,添加之后报错
infra/conf/serial: failed to parse json config > infra/conf: invalid field rule > infra/conf: failed to parse domain rule: geosite:geolocation-!cn > infra/conf: failed to load geosite: GEOLOCATION-!CN > infra/conf: failed to load file: geosite.dat > infra/conf: failed to open file: geosite.dat > open /var/mobile/Containers/Data/PluginKitPlugin/9596130A-14AF-4E66-980C-3AB70A6CA0CB/Library/Caches/geosite.dat: no such file or directory
找不到.dat 然后我添加了geoip.dat和geosite.dat这两个
PacketTunnelProvider中添加了
let filePath = MGConstant.assetDirectory.appendingPathComponent("geoip.dat")
do {
try data.write(to: filePath)
print("文件已保存到: (filePath)")
} catch {
print("保存文件时出错: (error)")
}

    let filePathGeosite = MGConstant.assetDirectory.appendingPathComponent("geosite.dat")

       do {
           try data.write(to: filePathGeosite)
           print("文件已保存到: \(filePathGeosite)")
       } catch {
           print("保存文件时出错: \(error)")
       }

进行了文件存储,然后再次连接发现报错
infra/conf/serial: failed to parse json config > infra/conf: invalid field rule > infra/conf: failed to parse domain rule: geosite:geolocation-!cn > infra/conf: failed to load geosite: GEOLOCATION-!CN > infra/conf: list not found in geosite.dat: GEOLOCATION-!CN
目前截止到这里没有找到更好的解决办法,希望您能给一些建议
望回复

求助

您好,现在遇到使用libxray打包IOS的xcframework,在项目中引入,却无法连接成功,想问下您是否可以提供一个可以参考的xray ios项目吗?十分感谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.