How do you tell if a key exists for an object using Key-Value Coding?

后端 未结 4 764
予麋鹿
予麋鹿 2021-01-31 14:44

I\'d like to test whether an object has a writeable @property in the iPhone SDK.

One possible way of doing this is to check the -valueForKey: method, but that seems rat

4条回答
  •  太阳男子
    2021-01-31 15:41

    The try/catch approach you propose in your question is the only reliable way of knowing whether valueForKey: will throw an exception.

      @try {
        id *value = [instance valueForKey:@"myProperty"];
      }
      @catch (NSException * e) {
        // Key did not exist
      }
    

提交回复
热议问题