1

I just want to confirm when to weak link in IOS

Case 1: A totally new framework.
Solution: Weak link the framework if you need to support previous versions and handle that gracefully in code. This is fine.

Case 2: Existing framework and class but new selector addition and you are using this new selector.
Solution: No need to weak link . Just check if respond to selector and handle the code. This is also fine.

Case 3: Existing framework but new class added and you are using this new class.
Solution: Weak link the framework if you need to support previous versions and handle that gracefully in code . is this correct ??

I tried and confirmed case 1 and 2. For case 3 i tried using UIStepper class but unfortunately it was hidden in previous implementation :-), so couldn't confirm.

see the following link: UIStepper not shown but did not crash in 4.3 simulator

Thought of leaving the question to GURUs for a quick answer.

Please confirm case 3

-mia

Community
  • 1
  • 1
mia
  • 373
  • 1
  • 4
  • 15

1 Answers1

2

For case 3 (new class in an existing framework), you do not need to weak link to the framework, but you will need to use NSClassFromString to get a reference to the Class in order to create instances of it (and remember to handle the case where it returns nil on older versions of iOS).

一二三
  • 21,059
  • 11
  • 65
  • 74
  • see the below link (http://stackoverflow.com/questions/2618889/universal-iphone-ipad-application-debug-compilation-error-for-iphone-testing/2622027#2622027) "That error is being triggered because you didn't weak-link the UIKit framework. The UIKit framework in iPhone OS 3.2 added the UISplitViewController, and if you link it in as normal your application will assume those symbols exist on 3.0, where they don't." Why such an explanation ? i am confused. – mia Feb 02 '12 at 10:35
  • As of iOS 4.2 there are two methods for doing this; but in both cases, if you try to use the symbol without first checking for its existence, your app will crash on older versions of iOS. – 一二三 Feb 02 '12 at 10:44
  • @ 一二三 I understand that .. but above case says about addition to UIKit Framework . As per your answer just NSClassFromString would be enough for additions , but here weak linking is mentioned. Is that answer wrong ?? Am i missing something here ? Can you please elaborate a little more ? – mia Feb 02 '12 at 11:49
  • They are two valid solutions to the same problem. Prior to iOS 4.2, `NSClassFromString` was the only solution; but now either `NSClassFromString` or checking `[newclass class] != nil` and weak-linking to the framework is acceptable. – 一二三 Feb 03 '12 at 01:48
  • Sorry .. still a small doubt .. See these 3 cases (1)-- Prior to IOS 4.2, NSClassFromString can be used . (2)-- Prior to IOS 4.2 , if you need to subclass the new class , NSClassFromString and weak linking is required . (3)-- From IOS 4.2 only [newclass class] != nil is required. See this link -- http://www.marco.org/2010/11/22/supporting-older-versions-of-ios-while-using-new-apis -- am i correct ?? or in (3) is it like inorder to support that feature we need to weak link ?? sorry if i am annoying you .. – mia Feb 03 '12 at 04:21
  • Yes, in order to support `[newclass class] != nil`, you need to weak link. – 一二三 Feb 03 '12 at 10:44