1月 06

swift2 异常处理

enum Error : ErrorType {
    case DivisionError
}
func divide(a: Int, b: Int) throws -> Int {
    if b == 0 {
        //使用throws来指定方法来抛出一个错误
	throw Error.DivisionError
    }
    return a/b
}

do {
    let result = try 
    //运行相应的代码
    divide(a: Int, b: Int)
} catch let Error. DivisionError {
    print("error")
}

转载收藏地址
http://appventure.me/2015/06/19/swift-try-catch-asynchronous-closures/

12月 28

raspberry pi2 连接蓝牙键盘

B.O.W航世 无线蓝牙键盘 + raspberry pi 2 + 迷你USB蓝牙4.0适配器 无线接收器 CSR稳定芯片
CSR4.0
安装蓝牙程序
apt-get install -y bluetooth bluez-* blueman

使用命令lsusb,看看有没有识别usb蓝牙模块。我的信息如下:
Bus 001 Device 004: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

重启操作系统或用命令开启蓝牙服务。
#/etc/init.d/bluetooth start

开启键盘蓝牙扫描,使用“hcitool scan”命令搜索到键盘。我的信息如下:
87:73:00:00:65:62 Bluetooth 3.0 Keyboard

开始在界面测试多次,都是4位配对码,总是失败。
在管理员模式下使用buletoothctl命令行进行连接操作。
# bluetoothctl
进入命令行控制台
[bluetooth]# power on // Power on the Bluetooth device
[bluetooth]# agent on // Start the agent
[bluetooth]# default-agent // Set agent as the default one
[bluetooth]# pairable on // Allow pairing
[bluetooth]# scan on // Enable scan so discoverable devices will be displayed
[bluetooth]# pair 87:73:00:00:65:62 // Pair with a discovered device
[bluetooth]# connect 87:73:00:00:65:62

根据提示,输入6位配对码。连接成功,键盘可用。
重启自动加载,编辑/etc/rc.local增加下面命令即可。
bluetoothctl connect 87:73:00:00:65:62

raspberrypi-bluekey

参考:
https://wiki.tizen.org/wiki/Bluetooth

12月 02

swift2 tableview 滑动删除

部分主要代码

    @IBOutlet weak var userTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //注册缓存标示
        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "松手刷新数据")
        userTableView.addSubview(refreshControl)
        refreshData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func loadView() {
        super.loadView()
    }
    
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    // 删除部分开始
    //滑动删除必须实现的方法
    func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,
        forRowAtIndexPath indexPath: NSIndexPath) {
            let index = indexPath.row
            self.dataArray.removeAtIndex(index)
            self.userTableView.deleteRowsAtIndexPaths([indexPath],
                withRowAnimation: UITableViewRowAnimation.Top)
    }
    
    //滑动删除
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.Delete
    }
    
    //修改删除按钮的文字
    func tableView(tableView: UITableView,
        titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
            return "删除"
    }
    //删除部分结束
12月 01

golang 1.5.1 协程学习测试

golang的协程和多cpu部分再做个学习测试。算法还是使用http://www.simonzhang.net/?p=2789。
做了三个测试。

一、修改了之前单次循环部分。计算中,使用了break(就是delphi和C里的goto)。统一最后返回值,这样只有一个return。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b

二、单cpu循环8次,因为测试服务器cpu有8核,这样方便测试多cpu下的效果。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
        "runtime"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b

三、多cpu,每个cpu跑一次。使用协程处理。

/*
# Date:        2015-11-19
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------
*/

package main

import (
        "strconv"
        "runtime"
)

var all int = 1E7 //1E7 = 10^7 = python 10**7

func check(num int) bool{
    status := true
    str:=strconv.Itoa(num)
    var str_len int = len(str)
    var a int=str_len-1
    LOOP:
    for b:=0; b=loopnum{
            break
        }
  } 
}

运行结果:
一、
real 0m0.552s
user 0m0.656s
sys 0m0.028s
二、
real 0m4.277s
user 0m5.260s
sys 0m0.096s
三、
real 0m1.434s
user 0m10.605s
sys 0m0.040s

使用协程,多cpu并行处理效果不错。写法简单。第三个测试,因为是多核运行,所以把每核的使用加再一起计算。
10.605秒除8,单个执行速度约为1.324秒,协调所消耗的资源也不小。个人理解,如果运算量比较大还是多进程为好,
前端使用负载进行协调处理。

golangstudy20151127