class-extensions

Visibility of properties in Class Extensions and inheritance in Objective C

北慕城南 提交于 2019-12-10 12:47:55
问题 Say I have 2 classes: Money and Dollar, where Dollar inherits from Money. Money has a property declared in a class extension: #import "Money.h" @interface Money () @property(nonatomic) NSUInteger amount; @end @implementation Money @end Dollar inherits this property, but it's not visible from methods in Dollar. I can "fix", by redeclaring the property in Dollar.m: @interface Dollar () @property(nonatomic) NSUInteger amount; @end @implementation Dollar } @end I really dislike this and I'm not

How can I extend the Array class and keep its implementations

时光毁灭记忆、已成空白 提交于 2019-12-08 18:53:29
I'd like to add some functions to the Array class (I'd rather not have them as functions external to the class since it would ideally be discoverable when typing . following the object). This is what I have so far: export class List<T> extends Array<T> { constructor(items?: Array<T>) { super(...items) Object.setPrototypeOf(this, List.prototype); } get first(): T { return this[0] } } this runs fine: const list = new List([1,2,3]); console.log(list.first) but if I try to run this: const list = new List([1,2,3]); console.log(list.map(x=>x*2)) I get the following error: super(...items) ^ TypeError

How can I extend the Array class and keep its implementations

江枫思渺然 提交于 2019-12-08 05:47:35
问题 I'd like to add some functions to the Array class (I'd rather not have them as functions external to the class since it would ideally be discoverable when typing . following the object). This is what I have so far: export class List<T> extends Array<T> { constructor(items?: Array<T>) { super(...items) Object.setPrototypeOf(this, List.prototype); } get first(): T { return this[0] } } this runs fine: const list = new List([1,2,3]); console.log(list.first) but if I try to run this: const list =

How a class extension works as a means of implementing private methods

≯℡__Kan透↙ 提交于 2019-12-06 06:25:06
I believe a popular way to declare "private methods" in Objective-C is to create its class extension and declare methods that you would like to make as private. I would like to know more in detail on how an class extension makes the methods work as private. Update: I asked this question with the term empty category which is incorrect. I now changed it as class extension That's not an "empty category", it's a class extension . Read Bbum's explanation of them at the link I provided. That's because you create your empty category in your implementation file, not your header file so other classes

What's the harm of property override in Objective-C?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:53:05
There are several situations where you might override a super class's property. You declare a property with the same name and same attribute of its superclass'.(since if you change the attribute you can get an compiler warning).And you can synthesieze with an ivar that you create. What's the use of this? Or what's the harm can it do? If a superclass declares a property in a class extension (a category with no name), then it might not be in the header file. If you don't know that property from the header file, you can declare the same name property with what ever attribute or class you want.

Use Class Extension for Selective Visibility in Objective-C

时光怂恿深爱的人放手 提交于 2019-11-29 18:47:04
问题 Would it make any sense to put class extensions in their own .h files and #import them selectively to get various levels of visibility for a class' methods and properties? If this is a bad idea (or would not work), why? 回答1: It is a great idea and exactly why Class Extensions were designed (and why they are different than categories). Namely, you can: Foo.h @interface Foo:NSObject ...public API here... @property(readonly, copy) NSString *name; @end Foo_FrameworkOnly.h @interface Foo()

Objective-C: Should I declare private methods?

爱⌒轻易说出口 提交于 2019-11-29 10:04:18
I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C . But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just implement it, the app compiles and runs without warning or error. So, should I even bother declaring private methods in class extensions? Why should we have to declare methods anyway? In Java, you don't... neither in Ruby. A method definition only needs to be defined if the caller is declared before the method. For consistency I would recommend defining

Objective-C class extension

二次信任 提交于 2019-11-28 19:09:39
As a fairly new objective-c programmer (with a 4 years Java experience), I seem to be having a hard time understanding when to use class extensions. From what I understood (and please, correct me if I'm wrong), the main difference between categories and extensions is that the extension expects you to implement the methods inside your main implementation, whereas with a category, it can be in another implementation. It also seems that people are using extensions mainly for private methods. Here's my first question. What's the difference between using a class extension to declare a private

Identity 2.0: Creating custom ClaimsIdentity eg: User.Identity.GetUserById<int>(int id) for Per Request Validation

纵饮孤独 提交于 2019-11-28 09:29:47
See this similar question: Need access more user properties in User.Identity I would like to create custom authentication methods to use with my Razor Views that allows easy access IdentityUser properties relational to the User.Identity object but I am not sure how to go about it. I want to create several custom extensions similar to User.Identity.GetUserName() , User.Identity.GetUserById() , etc... instead of using this ViewContextExtension method. My Authentication type is currently the default type DefaultAuthenticationTypes.ApplicationCookie from VS2013 MVC5 template. As Shoe stated, I

Objective-C: Should I declare private methods?

和自甴很熟 提交于 2019-11-28 03:43:52
问题 I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C. But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just implement it, the app compiles and runs without warning or error. So, should I even bother declaring private methods in class extensions? Why should we have to declare methods anyway? In Java, you don't... neither in Ruby. 回答1: A method definition only