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



Clound SSD Virtual Server

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

Objective-C Property(strong,nonatomic) , Weak , Strong type (iOS, iPhone, iPad) การเขียน App โปรแกรมบน iOS สำหรับ iPhone/iPad บนภาษา Objective-C ในการประกาศตัวแปร Variable และ Property ผ่านการสร้างจาก IBOutlet เราจะเห็นว่าบางครั้งโปรแกรม Xcode มีการสร้าง Keyword ต่าง ๆ เช่น @property (strong,nonatomic) หรือ __strong หรือ __weak ตัวแปรเหล่านี้มันคืออะไร ?? และบางครั้งเราก็ไม่จำเป็นจะต้องใส่มัน แต่มันก็ยังทำงานได้ตามปกติ เรามาทำความรู้จักคร่าว ๆ ว่ามันคืออะไร และแต่ล่ะตัวมีหน้าที่อะไร และมีผลอะไรต่อโปรแกรมที่เราจะเขียนขึ้นมาบ้าง

Objective-C


บทความนี้จะอธิบายเพียงแค่ความหมายคร่าว ๆ เท่านั้น ส่วนรายละเอียดลึกจริง ๆ อาจจะต้องไปหาอ่านจากเว็บภาษาอังกฤษของ Apple โดยตรง หรือถ้ามีเวลารอให้เก่งและรู้มากกว่านี้ก่อน อาจจะมาเขียนเพิ่มเติมให้ครับ

ปกติแล้วเราจะเปิดตัวแปร Method หรือ Function ต่าง ๆ เราจะประกาศไว้ที่ Class ที่นามสกุล .h เช่น

@interfaceViewController : UIViewController { // ตำแหน่งสำหรับตัวแปรต่าง ๆ } // ตำแหน่งสำหรับ ประกาศ property และ method (function) @end


Ex 1
ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{

    IBOutlet UITextField *txtName;

    __strong IBOutlet UITextField *txtEmail;

    __weak IBOutlet UILabel *lblResponse; 

}

@property (strong,nonatomic) NSString *strName;

- (IBAction)OKClick:(id)sender;

@end

จากตัวอย่างจะเห็นว่า ตำแหน่งของตัวแปร มีกาศประกาศแบบปกติ และ แบบ _week และตำแหน่งของ property และ method จะมีการประกาศ @property (strong,nonatomic) เรามาทำความเข้าในกับความหมายของมัน

__strong (Strong type)
เป็นการประกาศค่าตัวแปรที่เป็นค่า Default ที่ประกาศด้วย __strong หรือจะไม่ประกาศก็จะมีค่าเป็น Stong เป็นปริยาย โดย Strong type หมายถึงค่าตัวแปร จะยังอยู่ตลอดเวลา ไม่หายหรือถูกเปลี่ยนแปลงเป็นค่าอื่น และสามารถที่จะเรียกใช้ได้ตลอดเวลา ข้อเสียของ Strong type คือโปรแกรมอาจจะใช้ Memory จำนวนมากโดยไม่มีการเคลียร์หรือทำให้เป็น nil ซึ่งก็มีโอากาสให้เกิด Memory Leak เช่นเดียวกัน

__weak (Weak type)
เป็นการประกาศค่าตัวแปรที่จะต้องระบุด้วย __weak โดยค่าตัวแปรที่ถูกประกาศด้วย Strong type ที่เป็น Pointer เมื่อค่าที่กำลังชี้ไปยังตัวแปรอื่น ๆ ด้วย Pointer ถูกเปลี่ยน ค่าตัวแปรที่ถูกประกาศด้วย _weak ก็จะถูกเปลี่ยนเป็น nil หรือค่าว่างขึ้นมาทันที








Ex

NSString *a = @"strong";
__strong NSString *b = a;
a = @"A is Strong";

ผลลัพธ์ที่ได้คือ a => A is Strong และ b => strong

แต่ถ้า b เป็น weak type ผลที่ได้จะต่างออกไปดังนี้

NSString *a = @"strong";
__weak NSString *b = a;
a = @"A is still Strong";

ผลลัพธ์ที่ได้คือ a => A is still Strong และ b => nil

จากตัวอย่างที่ 2 จะเห็นค่าตัวแปร b ซึ่งเป็น Pointer ชี้ไปยังค่าตัวแปร a และเมื่อ a มีการเปลี่ยนแปลงค่า ดังนั้นตัวแปร b ซึ่งเป็น Weak type ก็จะถูกเปลี่ยนเป็น nil (ว่าง) ในทันที


@property (strong,nonatomic)
เป็นการประกาศ property ของ class โดยหลัก ๆ จะอยู่ 2 อารกิวเมนต์ โดย อารกิวเมนต์แรกจะมีค่าระหว่าง strong / weak และ อารกิวเมนต์สองจะมีค่าระหว่าง nonatomic / atomic

nonatomic ไม่ให้ตัวแปลทำงานในแบบ thread
atomic ให้ตัวแปลทำงานในแบบ thread

และถ้าไม่กำหนดจะมีการเป็น strong และ atomic

สำหรับความหมายคร่าว ๆ ก็มีเพียงเท่านี้ ส่วนการนำไปใช้งานตรงไหน และใช้ยังไงยังไงให้เหมาะสมกับการเขียนโปรแกรม ไว้บทความหลัง ๆ จะได้อธิบายและยกตัวอย่างการใช้งานประกอบ

ผิดพลาดประการใดขออภัยมา ณ ที่นี้ด้วยครับ

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-21 17:15:58 / 2017-03-25 22:49:31
  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 DataType ชนิดของตัวแปรในภาษา 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 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 03
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 อัตราราคา คลิกที่นี่