Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > [iOS/iPhone] Tutorials - สอนเขียน iPhone App ฟรี เขียน iPad App เรียน iPhone เขียนโปรแกรม iPhone > Objective-C and DataType ชนิดของตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)



Clound SSD Virtual Server

Objective-C and DataType ชนิดของตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)

Objective-C and DataType ชนิดของตัวแปรในภาษา Objective-C (iOS,iPhone,iPad) ในภาษา Objective-C ชนิดของตัวแปร (DataType) มีอยู่หลายประเภท และการเลือกใช้ตัวแปรให้ถูกต้อง เป็นสิ่งที่จำเป็นอย่างยิ่งในการเขียนโปรแกรม เพราะจะช่วยลดความผิดพลาดในระหว่างการทำงาน ซึ่งปกติแล้วในกรณีที่ใช้ชนิดของตัวแปรไม่ถูกต้อง บางค่าจะไม่สามารถทำการ Build หรือ Compile ได้อยู่แล้ว แต่อาจจะมีตัวแปรบางตัวที่เกิดในกรณีที่ Runtime ขึ้นในขณะที่กำลังทำงาน และบทความนี้เราจะมารู้ชนิดของตัวแปร และการใช้ตัวแปรแบบง่าย ๆ บนภาษา Objective-C ในการที่จะพัฒนาโปรแกรมบน iOS สำหรับ iPhone และ iPad

Objective-C and Variable รู้จักกับตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)


- String DataType
ตัวแปรชนิดของ String หรือข้อความ เช่น This is my string ในการประกาศชนิดตัวแปรแบบ String จะต้องมีเครื่องหมาย @ นำหน้าเสมอ

    NSString *myString1;
    myString1= @"This is my string 1";
    NSString *myString2 = @"This is my string 2";

ประกาศตัวแปรชื่อ myString1 และ myString2

Ex
NSString *myString = @"This is my string";
NSLog (@ "Hello %@ " , myString);

Hello This is my string



- Integer DataType
ชนิดตัวแปรแบบจำนวนเต็มเช่น 1 และ 200 และ 1234 และ -321 (จำนวนเต็มลบ และ จำนวนเต็มบวก) และอื่น ๆ ที่ไม่มีทศนิยม
int a;
int b = 123;
int c;
c = 10;
ตัวอย่างการประกาศชนิดของแปรแบบ Integer

short int a;
long  int b = 123;
int c;
c = 10;

เราสามารถทำการกำหนด Size ของตัวแปรได้ดังรูป (short , long , ... สามารถดูขนาด Size ของแต่ล่ะตัวได้จากข้างล่างบทความ)

จากตัวอย่างตัวแปร a และ c มีชนิดเดียวกัน แต่ size จะไม่เท่ากัน และในกรณีที่เรากำหนดค่าตัวแปรเป็น short int หรือ long int เราสามารถกำหนดชนิดเป็น short และ long โดยไม่ต้องกำหนด int ซึ่งจะมีขนาด Size เท่ากัน

short int a;
short b;

long int c;
long d;

ตัวแปร a,b มี Size เหมือนกัน และ c,d ก็มี Size เท่ากัน

Ex
    int a,b;
    a = 123;
    b = 456;
    int c = a + b;
    
    NSLog (@ "Total %d + %d = %d " , a,b,c);

Total 123 + 456 = 579









- char DataType
เป็นชนิดของข้อมูลใช้เก็บข้อมูลอักษรเดียว หรืออักขระเดียว
    char myChar1 = 'A';
    char myChar2 = '5';
    char myChar3 = ';';

กำหนดตัวแปรแบบ char ซึ่งเก็บตัวค่าตัวแปรแค่ 1 ตัวอักษรเท่านั้น

Ex
    char myChar1 = 'A';
    char myChar2 = '5';
    char myChar3 = ';';
    
    NSLog (@ "Character myChar1 = %c , myChar2 = %c , myChar1 = %c  " , myChar1,myChar2,myChar3);

Character myChar1 = A , myChar2 = 5 , myChar1 = ;

นอกจากนี้ char ยังใช้เก็บค่าในรูปแบบของอักขระพิเศษอื่น ๆ พวก Special Characters/Escape Sequences เช่น \n , \r

\a - Sound alert
\b - Backspace
\f - Form feed
\n - New line
\r - Carriage return
\t - Horizontal tab
\v - Vertical tab
\\ - Backslash
\" - Double quote (used when placing a double quote into a string declaration)
\' - Single quote (used when placing a double quote into a string declaration)


Ex
    char myChar1 = 'A';
    char myChar2 = '5';
    char newline = '\n';
    char myChar3 = ';';
    
    NSLog (@ "Character myChar1 = %c , %c myChar2 = %c , myChar1 = %c  " , myChar1,newline,myChar2,myChar3);

Character myChar1 = A ,
myChar2 = 5 , myChar1 = ;




- float and double DataType
เก็บชนิดของตัวแปรที่เป็นค่าทศนิยม โดย float และ double จะคล้าย ๆ กัน แต่แตกต่างกันตรงที่ double จะสามารถเก็บได้มากกว่า float จำนวน 2 เท่า (สามารถดูได้จากตางรางข้างล่าง)

    float a = 123.456;
    double b;
    b = 456.789;

ประกาศชนิดตัวแปร float a; และ double b; โดยมีค่าตัวแปรตามที่กำหนด

Ex
    float a = 123.456;
    double b;
    b = 456.789;
    
    double c = a + b;
    
    
    NSLog (@ "Total %f + %f = %.2f" , a,b,c);

Total 123.456001 + 456.789000 = 580.25




- Boolean DataType
เก็บค่าตัวแปรที่เป็นค่า True (จริง) False (เท็จ) โดยในการจัดเก็บจริง ๆ จะมองเป็นค่า 1 และ 0

    BOOL boolOne = YES ;
    _Bool boolTwo = NO;
    bool boolThree = 0;

สามารถใช้ได้ทั้ง BOOL , _Bool และ bool

Ex
    BOOL boolOne = YES ;
    _Bool boolTwo = NO;
    bool boolThree = 0;
    
    
    NSLog (@ "boolOne = %i , boolTwo = %i , boolThree =%i  " , boolOne , boolTwo , boolThree);

boolOne = 1 , boolTwo = 0 , boolThree =0




Convert and Casting DataType
ในการ Covert ชนิดของข้อมูลในภาษา Objective-C ใช้เหมือนกับภาษา C หรือ C# เช่น (int)variable , (double)variable

Ex
    NSString *a = @"123";
    float b = 456;
    int c = (int)[a intValue] + (int)b;
    
    NSLog (@ "Total %d + %d = %d " , (int)[a intValue],(int)b,c);

Total 123 + 456 = 579


signed / unsigned DataType


สรุป Size และขนาดที่จัดเก็บ
char ตั้งแต่ −127 ไปจนถึง +127
short ตั้งแต่ −32767 ไปจนถึง +32767
int ตั้งแต่ −2,147,483,648 ไปจนถึง 2,147,483,647
long ตั้งแต่ −2,147,483,648 ไปจนถึง 2,147,483,647
long long ตั้งแต่ −9223372036854775808 ไปจนถึง +9223372036854775807
float ตั้งแต่ 1×10−37 ไปจนถึง 1×10 37
double ตั้งแต่ 1×10−308 ไปจนถึง 1×10 308









แหล่งความรู้เพิ่มเติม
http://www.macfeteria.com/blog/2012/08/07/objective-c-programming-chapter-2/
http://www.techotopia.com/index.php/Objective-C_2.0_Data_Types


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-21 07:15:18 / 2017-03-25 22:52:40
  Download : No files
 Sponsored Links / Related

 
การเขียน Coding ด้วยภาษา Objective-C กับการแสดงผลง่าย ๆ บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Dynamic Create Object Dynamically (Objective-C , iPhone)
Rating :

 
Objective-C and Cocoa กับการเขียนโปรแกรมบน iOS สำหรับ iPhone และ iPad
Rating :

 
Objective-C and Variable รู้จักกับตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C and Loop การสร้างลูปและแสดง Loop แบบง่าย ๆ (iOS,iPhone,iPad)
Rating :

 
Objective-C and if , else if , switch Flow Condition Statement (iOS,iPhone,iPad)
Rating :

 
Objective-C and Operator การใช้ Operator บนภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C Property(strong,nonatomic) , Weak , Strong type (iOS, iPhone, iPad)
Rating :

 
Objective-C and Class Object (OOP) การเรียกใช้งาน Class (iOS,iPhone,iPad)
Rating :

 
iOS/iPhone Substring / Split / Replace String (Objective-C)
Rating :

 
iOS/iPhone and Timer (NSTimer, Objective-C)
Rating :

 
iOS/iPhone and Thread (NSThread, Objective-C)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่