How Do I Declare A Block in Objective-C?

本文介绍了Objective-C中Block的基本用法,包括作为局部变量、属性、方法参数、方法调用参数及类型定义等不同场景下的使用方式。
转发链接:http://goshdarnblocksyntax.com


As a local variable:


returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
As a property:


@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);
As a method parameter:


- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
As an argument to a method call:


[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
As a typedef:


typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
This site is not intended to be an exhaustive list of all possible uses of blocks.
If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.


Unable to access this site due to the profanity in the URL? http://goshdarnblocksyntax.com is a more work-friendly mirror.
Lab 1 - Chapter 2 Chapter 2: Operators and Control Flow (Part 1) Expressions, Operators, and Basic Decisions Course: Java Language Programming Instructor: Ahsan Shehzad Date: September 8, 2025 Practical Session Objective Lab Goal: Give Our Application a Brain 🧠 We will add conditional logic to the MyContactManager project. Using if-else statements, the application will analyze a contact's data and print custom messages. Prerequisites: Java JDK 11 or higher installed. An IDE like IntelliJ IDEA or VS Code ready. Your project files from the previous lab. Final Result Preview: Step-by-Step Guided Exercise Setup: Prepare the Variables Before we can make decisions, we need data. We'll hard-code some variables in our main method to simulate having a single contact. 1. Goal: Define the data for our contact. 2. Instructions: 1. Open your Java project in your IDE. 2. Navigate to the main method in your main application file. 3. Declare and initialize variables to hold a contact's name, age, and friend status. 3. Code Block: Jane Doe is a friend. Categorized as: Adult Match found 1 2 34. Expected Result: The program should compile and run, printing only the "Processing contact..." message for now. Step 1: Implement the Friend Status Check Let's use a simple if-else statement to check the isFriend boolean variable. 1. Goal: Print a different message depending on whether the contact is a friend. 2. Instructions: 1. Below the variable declarations, type if (isFriend) . 2. The code inside the {} following the if will run only if isFriend is true . 3. Add an else block to handle the case where isFriend is false . 3. Code Block: 4. Expected Result: When you run the code, it should now print "Jane Doe is a friend." because isFriend is true . Try changing isFriend to false and see what happens! Step 2: Implement Age-Based Categorization Now, let's use a more complex if-else if-else chain to categorize the contact by age. 1. Goal: Print a message classifying the contact as a "Teenager," "Adult," or "Senior." 2. Instructions: 1. Add a new block of code after the friend-status check. public class MyContactManager { public static void main(String[] args) { // --- Contact Data --- String firstName = "Jane"; String lastName = "Doe"; int age = 32; boolean isFriend = true; System.out.println("Processing contact: " + firstName + " " + lastName); // We will add our logic below this line } } 1 2 3 4 5 6 7 8 9 10 11 12 13 // Inside the main method, after the variables... // --- Logic for Friend Status --- if (isFriend == true) { // You can also just write if (isFriend) System.out.println(firstName + " " + lastName + " is a friend."); } else { System.out.println(firstName + " " + lastName + " is not a friend."); } 1 2 3 4 5 6 7 82. Start with an if to check if age is less than 18. 3. Add an else if to check the next condition (age less than 65). 4. The final else will catch all remaining cases. 3. Code Block: 4. Expected Result: The program will now also print "Categorized as: Adult" since the contact's age (32) falls into the second condition. Review: The Complete Code Let's look at our complete main method with all the logic integrated. 1. Goal: Ensure all the pieces are in the right place. 2. Instructions: Compare your code with the block below to make sure it matches. 3. Code Block: // Inside the main method, after the friend status logic... // --- Logic for Age Categorization --- System.out.print("Categorized as: "); if (age < 18) { System.out.println("Teenager"); } else if (age < 65) { System.out.println("Adult"); } else { System.out.println("Senior"); } 1 2 3 4 5 6 7 8 9 10 11 public class MyContactManager { public static void main(String[] args) { // --- Contact Data --- String firstName = "Jane"; String lastName = "Doe"; int age = 32; boolean isFriend = true; System.out.println("Processing contact: " + firstName + " " + lastName); // --- Logic for Friend Status --- if (isFriend) { System.out.println(firstName + " " + lastName + " is a friend."); } else { System.out.println(firstName + " " + lastName + " is not a friend."); } // --- Logic for Age Categorization --- System.out.print("Categorized as: "); if (age < 18) { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204. Expected Result: A clean, readable main method that performs two distinct checks. Final Step: Compile and Run The moment of truth! Let's compile and run our application to see our logic in action. 1. Goal: Verify the final output. 2. Instructions: 1. Save your .java file. 2. Compile and run the program using your IDE's "Run" button or via the command line. 3. Check that the console output matches the expected result exactly. 3. Code Block (Console Output): 4. Expected Result: You have successfully implemented conditional logic! Your program now makes decisions based on the data it has. Challenge Task For Those Who Finish Early... 🚀 If you've completed the lab, try this challenge to test your understanding of the String class. Challenge: Implement a Search Feature 1. Add a new String variable to your main method called searchName and assign it a value (e.g., "Jane" ). 2. Write a new if-else statement that uses the .equals() method to compare searchName with the contact's firstName . 3. If they match, print "Match found" . 4. If they don't, print "No match." System.out.println("Teenager"); } else if (age < 65) { System.out.println("Adult"); } else { System.out.println("Senior"); } } } 21 22 23 24 25 26 27 28 Processing contact: Jane Doe Jane Doe is a friend. Categorized as: Adult 1 2 3Bonus: How could you make the search case-insensitive? (Hint: Look up the equalsIgnoreCase() method in the Java String documentation). 答案是什么
09-22
下载前可以先看下教程 https://pan.quark.cn/s/a4b39357ea24 Job-Recommend 蚂蚁集团招聘内推(校招、社招) 校招 蚂蚁集团 2023届实习生招聘开始啦~ 蚂蚁集团是中国最大的移动支付平台【支付宝】的母公司,也是全球领先的金融科技开放平台,致力于以科技推动包括金融服务业在内的全球现代服务业的数字化升级。 我们团队归属于支付宝事业群商家开放技术部,整个大团队致力于打造蚂蚁级的开放、产品、商户等通用业务平台,实现全局业务能力与商家资源的开放共享,对内助力于商家、用户、机构等服务体系构建,对外以小程序、生活号等为抓手激活支付+X的金融生活开放生态。 我们团队是城市化策略技术部,依托于亿级的商家及用户数据,通过数据、算法、工程构建城市智能决策网络及策略体系。 我们有耐心逗比的师兄,有温柔细致的师姐,有丰富多彩的团队活动,也有各式各样的员工关怀,快来联系我们吧! ! ! 招聘对象: 11-2023.10毕业的应届毕业生 招聘流程: 简历投递->在线笔试及测评->面试->发放实习offer->实习入职 岗位类型: 【Java研发岗位】 岗位要求: 本科及以上学历,计算机、通信、数据科学与大数据技术等相关专业。 熟练掌握java技术,对多线程、数据结构等有清晰的认识。 掌握常用数据结构、算法、设计模式,熟悉MySQL/Oracle数据库等关系型数据库。 具备较强的编程能力、数据分析能力、问题排查能力,工作主动,学习能力强。 【数据工程/数据挖掘岗位】 岗位要求: 1、对分布式计算有较深的认识, 熟练使用spark,hadoop等处理海量用户行为数据。 2、熟练运用python、shell等脚本编程语言。 3、熟练掌握概率论与数理统计者优先。 4...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值