العدّ التلقائي للمراجع (ARC)
استكشفوا كيفية تبسيط ARC لإدارة الذاكرة وتأثيره في تطوير Objective-C والتعليمات البرمجية القديمة.
العدّ التلقائي للمراجع (ARC) درس مجاني في Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Objective-C iOS Development for Legacy & Enterprise Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Welcome to ARC
Memory management is crucial in programming. In Objective-C, you once had to manage memory manually, which could lead to errors like memory leaks or crashes.
Automatic Reference Counting (ARC) changed this by automating a lot of the work for you. It's a compiler feature, not a runtime garbage collector.
ARC: Compiler Magic
With ARC, the Xcode compiler automatically adds the necessary memory management code (like retain, release, and autorelease calls) into your app during compilation.
This means you no longer write these calls yourself. The compiler ensures objects are kept alive as long as they're needed and released when they're not.
Your First ARC Object
Let's see ARC in action. When you create an Objective-C object, ARC automatically manages its memory. You don't need to call release.
This simple example shows an NSString being created. ARC handles its deallocation when it's no longer referenced.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Create an NSString object
NSString *greeting = @"Hello, CoddyKit!";
NSLog(@"%@", greeting);
// ARC automatically manages the memory for 'greeting'.
// No explicit [greeting release] is needed here.
}
return 0;
}Properties & Strong References
When you declare properties for your objects, ARC uses strong references by default for Objective-C objects. A strong reference means the object is "owned" and will not be deallocated as long as there's at least one strong reference to it.
For primitive types (like int, float), you'd use assign.
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (strong, nonatomic) NSString *name;
- (void)sayHello;
@end
@implementation MyObject
- (void)sayHello {
NSLog(@"Hello, my name is %@", self.name);
}
// dealloc is called automatically when strong refs are gone
- (void)dealloc {
NSLog(@"MyObject %@ is deallocated.", self.name);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyObject *obj = [[MyObject alloc] init];
obj.name = @"Coddy"; // 'name' is a strong property
[obj sayHello];
// 'obj' goes out of scope, ARC releases it, dealloc is called.
}
return 0;
}Custom Cleanup with `dealloc`
Under ARC, you do not call [super dealloc] or release objects within your dealloc method. The compiler handles all Objective-C object releases.
You still use dealloc to clean up non-Objective-C resources, such as Core Foundation objects, C++ objects, or file handles.
#import <Foundation/Foundation.h>
@interface MyResourceHolder : NSObject
@property (strong, nonatomic) NSString *identifier;
@end
@implementation MyResourceHolder
- (instancetype)init {
self = [super init];
if (self) {
_identifier = @"My Unique ID";
NSLog(@"MyResourceHolder %@ created.", _identifier);
// Imagine opening a C file handle or allocating C memory here.
}
return self;
}
// ARC handles Objective-C objects. Use dealloc for other resources.
- (void)dealloc {
NSLog(@"MyResourceHolder %@ deallocated. (Closing C file handle, etc.)", _identifier);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyResourceHolder *holder = [[MyResourceHolder alloc] init];
// holder goes out of scope, ARC deallocates it.
}
return 0;
}Transitioning Legacy Projects
Many older Objective-C projects were written with Manual Retain-Release (MRR). Xcode provides a tool to help migrate these projects to ARC.
Go to Edit > Refactor > Convert to Objective-C ARC.... This automates much of the conversion, but manual adjustments may still be needed, especially for complex projects or mixed codebases.
Mixed Codebases
Sometimes, you might need to use non-ARC files within an ARC project (e.g., third-party libraries). Xcode allows this.
You can tell the compiler to compile specific files without ARC by setting the -fno-objc-arc compiler flag for those files in your project's Build Phases settings.
ARC & Core Foundation
ARC only manages Objective-C objects. It doesn't manage Core Foundation objects (like CFStringRef, CFArrayRef) which are C-based.
To convert between Objective-C and Core Foundation types, you use bridging casts: __bridge, __bridge_transfer, and __bridge_retained. These tell ARC how to handle ownership.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *ocString = @"Hello ARC!";
// __bridge_retained: transfer ownership from ARC to Core Foundation.
// CFRelease must be called manually later.
CFStringRef cfString = (__bridge_retained CFStringRef)ocString;
NSLog(@"Objective-C String: %@", ocString);
NSLog(@"Core Foundation String: %@", (__bridge NSString *)cfString);
// Manual release required for Core Foundation objects obtained via __bridge_retained
CFRelease(cfString);
}
return 0;
}ARC's Limits & Pitfalls
While ARC simplifies memory management, it doesn't solve all problems. It can't prevent retain cycles, where two objects strongly reference each other, preventing either from being deallocated.
This is where weak references come in, which we'll cover in the next lesson. Also, be mindful of C-style memory (malloc/free) which ARC doesn't manage.
ARC Knowledge Check
ARC significantly changed how we manage memory in Objective-C.
Which of the following is a primary benefit of using Automatic Reference Counting (ARC) in Objective-C development?
ARC Recap & What's Next
You've learned that ARC automates memory management by inserting retain and release calls at compile time. This vastly simplifies development and reduces common memory errors.
We also touched on managing Core Foundation objects and the importance of using dealloc for non-Objective-C resource cleanup.
Next, we'll dive into Weak vs. Strong References to understand how to prevent retain cycles, a key concept even with ARC.
الأسئلة الشائعة
هل درس «العدّ التلقائي للمراجع (ARC)» مجاني؟
نعم — نص درس «العدّ التلقائي للمراجع (ARC)» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Objective-C iOS Development for Legacy & Enterprise Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
ماذا ستتعلم في «العدّ التلقائي للمراجع (ARC)»؟
استكشفوا كيفية تبسيط ARC لإدارة الذاكرة وتأثيره في تطوير Objective-C والتعليمات البرمجية القديمة. تتمرن على Objective-C iOS Development for Legacy & Enterprise Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Objective-C iOS Development for Legacy & Enterprise Apps؟
لا تُشترط خبرة سابقة. Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «العدّ التلقائي للمراجع (ARC)»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Objective-C iOS Development for Legacy & Enterprise Apps هذا؟
نعم. كل درس في Objective-C iOS Development for Legacy & Enterprise Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات Manual Retain-Release (MRR)
- العدّ التلقائي للمراجع (ARC)
- المراجع الضعيفة مقابل القوية
- كسر دورات الاحتفاظ في Blocks