[Ruby]jsonを出力する

jsonを出力する

サンプル

#coding:utf-8
require 'json'
print("Content-type: application/json\n\n")

print JSON.generate({"time"=>Time.now.strftime("%Y-%m-%d %H:%M:%S"),
                    "user"=>[
                                {"id" => 32, "name"=>"jeff"},
                                {"id" => 56, "name"=>"john"}
                            ]
                    })

[objective-c]UIColorから16進数の文字列を得る

UIColorから16進数の文字列を得る

NSString*colorHex = [self getColorHexByUIColor:[UIColor redColor]];
NSLog("%@",colorHex);

-(NSString*)getColorHexByUIColor:(UIColor*)color
{
    CGFloat r,g,b,a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    return [NSString stringWithFormat:@"%02x%02x%02x",(int)(r*255.0),(int)(g*255.0),(int)(b*255.0)];
}

[Swift]Swift #5 animateWithDuration

Swift #5 animateWithDuration

@IBOutlet weak var mainLabel: UILabel!

func initUI(){
    var message:String = getMessage(message:"いあうえ",message2:"390u8")
    println(message)
        
    message = getMessage(message2:"むぎちゃ")
    println(message)

    UIView.animateWithDuration(
        2.0,
        delay: 1.0,
        usingSpringWithDamping: 1.0,
        initialSpringVelocity: 0.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations:{()in
            self.mainLabel.frame = CGRectMake(100.0, 300.0, self.mainLabel.frame.size.width, self.mainLabel.frame.size.height)
        },
        completion:{(finished)in
            println("finished:\(finished)")
            
        })
}
func getMessage(var message:String="ABC",var message2:String="MBNRKF")->String{
    return "\(message) \(message2)"
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

いあうえ 390u8
ABC むぎちゃ
finished:true

[Swift]Swift #4 カメラを扱う

Swift #4 カメラを扱う

Main.storyboard
Main.storyboard

ViewController

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    
    @IBOutlet var mainImageView: UIImageView
    var picker:UIImagePickerController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        picker = UIImagePickerController()
        picker!.delegate = self
        picker!.allowsEditing = false
    }

    @IBAction func onTapButton1(button:UIButton){
        println( "onTapButton - open album" )
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    @IBAction func onTapButton2(button:UIButton){
        println( "onTapButton - take photo" )
        
        if picker==nil {
            return
        }
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        println( "imagePickerController" )
        self.dismissViewControllerAnimated(true, completion: nil)
        mainImageView.image = image
        if picker!.sourceType == UIImagePickerControllerSourceType.Camera {
            println( "save photo" )
            UIImageWriteToSavedPhotosAlbum(image, self, "onSaveImageWithUIImage:error:contextInfo:", nil)
        }
    }
    
    func onSaveImageWithUIImage(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafePointer<()>){
        if error {
            println( "error" )
            return
        }
        println( "success" )
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController!){
        println( "imagePickerControllerDidCancel" )
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

[Swift]Swift #3

Swift #3

func initUI(){
    //配列-固定
    println( "-------------- array1" )
    let array1 = [0, 3, "あいうえお"]
    println( array1 )
    
    //配列-可変
    println( "-------------- array2" )
    var array2:[Int] = [1,2,3]
    array2[1]=57
    println( array2 )
    
    println( "-------------- array3" )
    var array3:[Float] = [4.0, 768.0, 0.432, 0.2]
    array3[0]=5.0
    array3[2]=346.0
    println( array3 )
    
    println( "-------------- array4" )
    var array4:[String] = ["か","き","く","け"]
    array4[2]="さしすせそ"
    println( array4 )
    
    //Dictionary
    println( "-------------- Dictionary" )
    var dic1:[String:NSObject?] = ["name":"た","address":"ち","number":"つ"];
    println( dic1 )
    println( dic1["address"] )
    dic1["address"]="マダガスカル"
    println( dic1 )
    
    //for in 1
    println( "-------------- for in 1" )
    for value in array2 {
        println( value )
    }
    
    //for in 2
    println( "-------------- for in 2" )
    for obj in dic1 {
        println( obj )
    }
    
    //for in 3
    println( "-------------- for in 3" )
    for i in 1..<8 {
        println( i )
    }
    
    //for in 4
    println( "-------------- for in 4" )
    for i in 1...8 {
        println( i )
    }
    
    //for in 5
    println( "-------------- for in 5" )
    for i in 0..<array3.count {
        println( "\(i) : \(array3[i])" )
    }
    
    //for in 6
    println( "-------------- for in 6" )
    for obj in dic1 {
        var (key,value)=obj
        println( "\(key) : \(value)" )
    }
    
    //for
    println( "-------------- for" )
    for var i=0;i<10;i++ {
        println( "for : \(i)" )
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

-------------- array1
(
    0,
    3,
    "\U3042\U3044\U3046\U3048\U304a"
)
-------------- array2
[1, 57, 3]
-------------- array3
[5.0, 768.0, 346.0, 0.200000002980232]
-------------- array4
[か, き, さしすせそ, け]
-------------- Dictionary
[number: つ, name: た, address: ち]
ち
[number: つ, name: た, address: マダガスカル]
-------------- for in 1
1
57
3
-------------- for in 2
(number, つ)
(name, た)
(address, マダガスカル)
-------------- for in 3
1
2
3
4
5
6
7
-------------- for in 4
1
2
3
4
5
6
7
8
-------------- for in 5
0 : 5.0
1 : 768.0
2 : 346.0
3 : 0.200000002980232
-------------- for in 6
number : つ
name : た
address : マダガスカル
-------------- for
for : 0
for : 1
for : 2
for : 3
for : 4
for : 5
for : 6
for : 7
for : 8
for : 9

[Swift]Swift #2

Swift #2

func initUI()
{
    //返り値
    println( getMessage() )
    
    //引数のデフォルト値
    println( getMessageOfJeff() )
    
    //Tuples(タプル)
    let message = (2014,"what are you talking about ?")
    let (year,question) = message
    println( message )
    println( "西暦\(year)" )
    println( "question:\(question)" )
}
func getMessage()->String{
    return "I am a pen."
}
func getMessageOfJeff(message:String="Is this a pen ?")->String{
    return message;
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

I am a pen.
Is this a pen ?
(2014, what are you talking about ?)
西暦2014
question:what are you talking about ?

参考
The Swift Programming Language: The Basics

Tuples can return multiple values from a function as a single compound value.

[Swift]Swift #1

Swift #1

func initUI()
{
    //行の終わりに;(セミコロン)はいらない
    //定数
    let message = "Hello,world!"
    println(message)
    
    //変数
    var n = 34
    println("わたしは\(n*2)歳です")
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果

Hello,world!
わたしは68歳です

[objective-c]UILabelを複製する

UILabelを複製する

-(UILabel*)getCloneUILabelByUILabel:(UILabel*)label
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:label];
    return [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
UILabel*cloneLabel = [self getCloneUILabelByUILabel:modelLabel];

[JavaScript]プロパティ名を指定してソート

プロパティ名を指定してソート

function g_sortOn(_propertyName){
    return function(a,b){
        if( a[_propertyName] > b[_propertyName] )return 1;
        if( a[_propertyName] < b[_propertyName] )return -1;
        return 0;
    }
}
var tmp = targetArray.sort( g_sortOn('name') );