ไวยากรณ์และชนิดข้อมูลพื้นฐาน
เรียนรู้ไวยากรณ์พื้นฐาน ชนิดข้อมูลดั้งเดิม และอ็อบเจ็กต์ Objective-C ทั่วไปอย่าง NSString และ NSArray
ไวยากรณ์และชนิดข้อมูลพื้นฐาน เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Objective-C Basics
Time for the fundamentals: Objective-C's basic syntax and the data types you will work with every day.
Statements End with Semicolons
Like other C-based languages, every Objective-C statement ends with a semicolon. Forget one and Xcode will flag it fast.
Explain Your Code with Comments
Comments are notes the compiler ignores. Use // for a single line and /* */ for multi-line blocks.
Primitive Data Types
Primitive types hold simple values: int, float/double, char, and BOOL (which is YES or NO).
Primitives in Action
Here those primitive types come to life — declared, assigned, and printed with NSLog format specifiers.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int age = 30;
float price = 19.99f; // 'f' for float literal
double pi = 3.14159;
char initial = 'J';
BOOL isLoggedIn = YES;
NSLog(@"Age: %i", age);
NSLog(@"Price: %.2f", price);
NSLog(@"Pi: %f", pi);
NSLog(@"Initial: %c", initial);
NSLog(@"Logged In: %@", isLoggedIn ? @"YES" : @"NO");
}
return 0;
}Working with Text: NSString
For words and sentences you use the NSString object, not char. NSString is immutable: its contents never change after creation.
NSString Example
Declaring an NSString uses the @ prefix before the literal — a piece of syntax unique to Objective-C.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *greeting = @"Hello, CoddyKit!";
NSString *name = @"Alice";
NSString *message = [NSString stringWithFormat:@"Welcome, %@!", name];
NSLog(@"Greeting: %@", greeting);
NSLog(@"Personal Message: %@", message);
}
return 0;
}Collections: NSArray
NSArray stores an ordered list of objects, accessed by zero-based index. It is immutable; use NSMutableArray when you need to change it.
NSArray Example
Create an NSArray with the @[] literal. The older arrayWithObjects form needs a nil terminator.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Array literal syntax
NSArray *fruits = @[@"Apple", @"Banana", @"Cherry"];
NSLog(@"First fruit: %@", fruits[0]); // Access by index
NSLog(@"All fruits: %@", fruits);
// Another way to create (older, but good to know)
NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", nil];
NSLog(@"Colors: %@", colors);
}
return 0;
}Quick Check on Types
Consider the following Objective-C declarations. Which one correctly defines a boolean variable named isActive and sets it to true?
Recap: Syntax & Data Types
Recap: semicolons end statements, comments document, primitives like int and BOOL hold simple values, and NSString and NSArray handle text and lists.
คำถามที่พบบ่อย
บทเรียน “ไวยากรณ์และชนิดข้อมูลพื้นฐาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ไวยากรณ์และชนิดข้อมูลพื้นฐาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ไวยากรณ์และชนิดข้อมูลพื้นฐาน”
เรียนรู้ไวยากรณ์พื้นฐาน ชนิดข้อมูลดั้งเดิม และอ็อบเจ็กต์ Objective-C ทั่วไปอย่าง NSString และ NSArray คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ไวยากรณ์และชนิดข้อมูลพื้นฐาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Objective-C
- ไวยากรณ์และชนิดข้อมูลพื้นฐาน
- การตั้งค่า Xcode สำหรับ Objective-C
- การทำงานกับ NSString และชนิดข้อมูลของ Foundation