Thursday, May 27, 2010

Objective-C Part 1

Extension ใน objective-c
.h = header file ที่จัดเก็บ header file,function,type,constant,declaretion
.m = objective-c code และ c
.mm = ที่จัดเก็บ code ที่เป็น c++

การประกาศ class ใน objective-c

@interface Dog{
NSString *name;
NSString *origin;
}

//Region properties
@property (retain) NSString *name;
@property (retain) NSString *origin;
//Region method
-(void)run:(int) speed;
@end

การ Implement class

@Implement Dog
@synthesize name,origin;
-(void)run:(int) speed){
NSLog("Dog run speed %@",speed);
}
@end

ทั้ง interface และ implement class สามารถอยู่ไฟล์เดียวกันและเเยกไฟล์กันก็ได้
ถ้าเเยกไฟล์ interface class จะอยู่ใน .h
@synthesize เพื่อบอกให้ compiler gen setter และ getter method ของ properties

การสร้าง object สามารถทำได้โดย

Dog *dog1 =[[Dog alloc] init];


การเรียกเมธอดหรือส่งเมจเสจ

[dog1 speed:5];


โปรโตคอลและเดลิเกต
สัญญลักษณ์ ของโปรโตคอล @protocal โปรโตคอล คือ การประกาศเมธอดที่คลาสอื่นสามารถนำไป implement ได้
อาจจะคล้ายๆ กับ abstract ใน java ซึ่งตัวโปรโตคอลนี้ไม่สามารถเป็นคลาสได้ด้วยตัวมันเอง การประกาศโปรโตคอล
จะมีลักษณะคล้าย interface และ object ตัวอื่นสามารถที่จะตอบสนองเมธอดของโปรโตคอลได้ด้วย การ implement
เมื่อคุณ implement method ของ protocal ในคลาสและคลาสคุณจะทำงานร่วมกันได้กับโปรโตคอล
และลักษณะการประกาศโปรโตคอล

@protocal MyProtocal
-(void) myProtocalMethod;
@end


ิติดตาม Part 2 ต่อครับ

Saturday, May 22, 2010

การ Compile Objective-c บน Windows XP

เมื่อ Download GNUstep
เราก็มาเริ่มการเขียน Objectiv-C กันเลยครับ ก็คงหนีไม่พ้น Hello program สุดคลาสสิก


//First program example
#import

int main(int argc,const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");

[pool drain];
return 0;
}


แล้วก็เข้าไปที่ Start->All Programs->GNustep->shell นะครับ
เเล้วพิมพ์คำสั่งในการ compile ซึ่งจะต่างจากภาษา c เล็กน้อย แต่คล้ายๆ
เช็ตคลาสพาธเหมือน java
$gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

ยาวใหมละครับเหอะๆ ก็เเบบนี้ละครับ เราไม่มีเครื่อง Mac

Objective-C Programming

ทุกวันนี้ผลิตภัณฑ์ของ Apple Inc. เข้ามีบทบาทอย่างมากในวงการไอที
ไม่ว่าจะเป็น iPod,iPhone,iPad ซึ่งทำให้คนในวงการโปรเเกรมเมอร์
เริ่มขยับด้วยเข้าหา และที่ไม่พูดถึงไม่ได้เลยก็คือ Objective-c เป็น
ภาษา c และ smalltalk เเปลงร่างรวมตัวกันเป็น object oriented programming
เพื่อใช้ในการเขียน application บนผลิตภัณฑ์ของ Apple และผมก็เป็น
หนึ่งในนั้นที่ต้องการเขียน objective-c
เราเริ่มตันหาเครื่องมากันดีกว่าครับ
1. ใช้ VMWare แล้วลง Mac OSX image ไฟล์เอา ซึ่งตอนนี้กำลังโหลดอยู่ครับ
เดียวถ้าเสร็จจะมาเล่าให้ฟัง
2. ใช้ GNUstep ครับ
ตัวเอาไว้เรียนรู้การเขียน objective-c อย่างเดียวนะครับ เขียน application บน i ทั้งหหลายไม่ได้
จบแค่นี้ก่อนครับ

Tuesday, May 18, 2010

jQuery sortable with asp.net



การ sorttable จะใช้ jquery ui อยู่ 3 ไฟล์นะครับ คือ jquery.x.x.js ,ui.core.js,ui.sortable.js
ในส่วนของ form
  • ">
  • หน้าที่ #
    " width="120" height="64">
    ส่วนของ javascript
    $(document).ready(function(){
    $("#reorder").sortable({ handle:"span", start:sorting, stop:sortStop, update:updatePageNo });
    });

    function sorting(event,ui){
    $(ui.item).addClass("draging");
    //Set loading image
    $('#loading').html("").text("กรุณารอสักครู่...");
    }
    function sortStop(event,ui){
    $(ui.item).removeClass("draging");
    }
    function updatePageNo(event,ui){
    var idArr = $('#reorder').sortable('toArray');
    var dataTransfer;
    //var issueID = $("#HiddenIssueID").val();
    dataTransfer = "[";
    //Convert to json array
    jQuery.each(idArr,function(index){
    dataTransfer +="{\"ContentID\":"+this+",\"PageNo\":"+(index+1)+"},";
    });
    dataTransfer = dataTransfer.substring(0,dataTransfer.length-1);
    dataTransfer += "]";
    //$.toJSON(idArr);
    //Sys.Serialization.JavaScriptSerializer.serialize(idArr
    $.post(
    'UpdateReorderHandler.ashx?q='+dataTransfer,
    function(data) {
    //Remove loading image
    $('#loading').empty();
    if(data=="success"){
    alert("เเก้ไขข้อมูลการเรียงลำดับเรียบร้อยแล้ว");
    window.location = window.location;
    }else{
    alert("ไม่สามารถเเก้ไขข้อมูลการเรียงลำดับได้");
    $('#reorder').sortable('cancel');
    window.location = window.location;
    }
    }
    );
    }

    และส่วนของ Server
    context.Response.ContentType = "text/plain";
    string q = context.Request.QueryString["q"];
    JsonTextParser parser = new System.Net.Json.JsonTextParser();
    JsonObject obj = parser.Parse(q);
    JsonUtility.GenerateIndentedJsonText = false;
    string name = string.Empty;
    string value = string.Empty;
    JsonArrayCollection arrjSon = (JsonArrayCollection)obj;
    List PageList = new List();
    ContentPage page = null;
    foreach (JsonObject field in arrjSon)
    {
    List jsons = (List)field.GetValue();
    page = new ContentPage();
    foreach (JsonObject o in jsons)
    { //issue = new Issue();
    if (o.Name == "ContentID")
    page.ContentID = Convert.ToInt64(o.GetValue());
    if (o.Name == "PageNo")
    page.PageNo = Convert.ToInt32(o.GetValue());
    //if (o.Name == "IssueID")
    //issue.IssueID = o.GetValue().ToString();
    //page.Issue = issue;
    }
    PageList.Add(page);
    }
    if (_issueService == null)
    _issueService = new IssueService();
    try
    {
    if (_issueService.UpdateSequenceNo(PageList.ToArray()))
    { //Success
    context.Response.Write("success");
    }
    else
    {
    //Fail
    context.Response.Write("fail");
    }
    }
    catch (Exception)
    {
    context.Response.Write("fail");
    }


    Thursday, May 13, 2010

    asp.net(c#) upload file by ajax

    ก่อนอื่นให้ไปโหลดไลบรารีตัวนี้มาก่อนนะครับ valums.com/ajax-upload/
    copy code ส่วนนี้ไปว่างระหว่าง
    //
    $(document).ready(function(){
    var button = $('#upload_button'), interval;
    new AjaxUpload(button, {
    action: 'UploadHandler.ashx',
    name: 'myfile',
    responseType: 'json',
    onSubmit : function(file, ext){
    //if (! (ext && /^(mp3|wma|wmv|mp4|mpeg|dat)$/i.test(ext))){
    // extension is not allowed
    //alert('Error: invalid file extension');
    // cancel upload
    //return false;
    //}
    // change button text, when user selects file
    button.text('Uploading');
    // If you want to allow uploading only 1 file at time,
    // you can disable upload button
    this.disable();
    // Uploding -> Uploading. -> Uploading...
    interval = window.setInterval(function(){
    var text = button.text();
    if (text.length <>
    button.text(text + '.');
    } else {
    button.text('Uploading');
    }
    }, 200);
    },
    onComplete: function(file, response){
    button.text('Upload');
    window.clearInterval(interval);
    // enable upload button
    this.enable();

    // add file to the list
    if(response.Status=='success'){
    $('
  • ').appendTo('#result').html($("Delete"));
    }else{
    $('#result').append('
  • Fail to upload!
  • ');
    }
    }
    });
    });
    //Delete file
    function FileDelete(sender,path){
    var currentDate = new Date();
    var q = "q="+path+"&seek="+currentDate.getMilliseconds();
    var response = confirm("Are you sure?");
    if(response){
    $.ajax({
    dataType : "json",
    url: "FileDeleteHandler.ashx",
    data: q,
    success: function(msg){
    if(msg.status == "success"){
    $(sender).parents('li:eq(0)').hide();
    }else{
    alert('Error can not delete!');
    }
    },
    error : function(msg){
    alert('Error can not delete!');
    }
    });
    }
    return false;
    }
    //]]>

    ส่วนในform
    begin div id="upload_button"
    Upload
    end div
    ul
    id="result" style="list-style-type: none">
    ul

          หลังจากนั้นให้สร้าง GenericHandler ขึ้นมาครับ
          แล้วโค้ดในมธอด ProcessRequest เป็นเเบบนี้ครับ
          public void ProcessRequest(HttpContext context)
          {

          string strFileName = System.IO.Path.GetFileName(context.Request.Files[0].FileName);
          string strSaveLocation = context.Server.MapPath("ContentImage") + "" + strFileName;
          context.Request.Files[0].SaveAs(strSaveLocation);
          }

          Wednesday, May 12, 2010

          C# write binary to file

          FileInfo fileInfo = new FileInfo(filenamePath);
          if (!IsExistsDirectory(fileInfo.DirectoryName))
          {
          CreateDirectory(fileInfo.DirectoryName);
          }
          FileStream fs = new FileStream(filenamePath, FileMode.Create, FileAccess.Write);
          using (BinaryWriter bw = new BinaryWriter(fs))
          {
          bw.Write(buff);
          }