subclassing

Calling super.init() in initializer of NSObject subclass in Swift

谁说胖子不能爱 提交于 2019-12-04 23:28:35
I'm building an iOS app in Swift and drawing on the Lister sample project Apple provides. Lister uses two model objects: List and ListItem. I found that both of them do not call super.init() in their initializers even though they subclass NSObject. However, in the Objective-C version of Lister, both model objects (AAPLList and AAPLListItem) do call [super init] . The Swift Programming Language clearly states that “designated initializers must call a designated initializer from their immediate superclass.” (Rule 1 of Initializer Chaining in Initialization) What's going on here? Why is this an

Is there a way to create subclasses on-the-fly?

本秂侑毒 提交于 2019-12-04 23:15:09
问题 I'm creating a game in which I have a somewhat complex method for creating entities. When a level is loaded, the loading code reads a bunch of YAML files that contain attributes of all the different possible units. Using the YAML file, it creates a so-called EntityResource object. This EntityResource object serves as the authoritative source of information when spawning new units. The goal is twofold: Deter cheating by implementing a hash check on the output of the YAML file Aid in debugging

Proper way to replace NSTextStorage in NSTextView?

家住魔仙堡 提交于 2019-12-04 18:56:54
问题 I am making some text viewer app. Currently I need very frequent and precise line handling ability, so I want to subclass NSTextStorage class. But I couldn't find any method to set a new text storage to NSTextView . The only method I could find was -[NSLayoutManager replaceTextStorage:] method. But it's confusing whether this is what I was looking for. Because it seems just replace text storage of linked NSLayoutManagers instead of NSTextView. I also considered subclassing NSTextView and

overloaded __iter__ is bypassed when deriving from dict

随声附和 提交于 2019-12-04 17:34:49
Trying to create a custom case-insensitive dictionary, I came the following inconvenient and (from my point-of-view) unexpected behaviour. If deriving a class from dict , the overloaded __iter__ , keys , values functions are ignored when converting back to dict . I have condensed it to the following test case: import collections class Dict(dict): def __init__(self): super(Dict, self).__init__(x = 1) def __getitem__(self, key): return 2 def values(self): return 3 def __iter__(self): yield 'y' def keys(self): return 'z' if hasattr(collections.MutableMapping, 'items'): items = collections

Delphi: How to remove subclasses in reverse order?

跟風遠走 提交于 2019-12-04 09:58:49
Mike Lischke's TThemeServices subclasses Application.Handle , so that it can receive broadcast notifications from Windows (i.e. WM_THEMECHANGED ) when theming changes. It subclasses the Application object's window: FWindowHandle := Application.Handle; if FWindowHandle <> 0 then begin // If a window handle is given then subclass the window to get notified about theme changes. {$ifdef COMPILER_6_UP} FObjectInstance := Classes.MakeObjectInstance(WindowProc); {$else} FObjectInstance := MakeObjectInstance(WindowProc); {$endif COMPILER_6_UP} FDefWindowProc := Pointer(GetWindowLong(FWindowHandle, GWL

Curiously Recurring Template and Template parameter dependent subclassing issues

 ̄綄美尐妖づ 提交于 2019-12-04 09:43:18
I am trying to make the following code work template < class __derived, class __object = typename __derived::Object > struct Base { using Derived = __derived; using Object = __object; void function(Object o) { return Derived::function(s); } } //template < class __derived > //struct Base { // using Derived = __derived; // using Object = typename Derived::Object; // void function(Object o) { return Derived::function(s); } //} template < class __object > struct Derived : public Base< Derived< __Object > > { using Object = __object; void function(Object o) { ... } } And i instantiate an object by

Subclassing and built-in methods in Python

白昼怎懂夜的黑 提交于 2019-12-04 05:58:31
For convenience, I wanted to subclass socket to create an ICMP socket: class ICMPSocket(socket.socket): def __init__(self): socket.socket.__init__( self, socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) def sendto(self, data, host): socket.socket.sendto(self, data, (host, 1)) However, I can't override socket.sendto : >>> s = icmp.ICMPSocket() >>> s.sendto <built-in method sendto of _socket.socket object at 0x100587f00> This is because sendto is a "built-in method". According to the data model reference , this is "really a different disguise of a built-in function, this time

How to subclass a subclass of numpy.ndarray

旧巷老猫 提交于 2019-12-04 05:16:56
I'm struggling to subclass my own subclass of numpy.ndarray. I don't really understand what the problem is and would like someone to explain what goes wrong in the following cases and how to do what I'm trying to do. What I'm trying to achieve: I have a subclass of numpy.ndarry that behaves as I want (class A in the code below). I want to subclass A (class B in the code below) so that B contains additional information (name) and methods (the decorated .simple_data method). Case 1: import numpy as np class A(np.ndarray): def __new__(cls,data): obj = np.asarray(data).view(cls) return obj def _

Subclassing models in Rails

不羁的心 提交于 2019-12-04 04:29:50
I have two models, Article and Recipe, which have a bunch of the same attributes and methods. I want to make the subclasses of a new class "Post" and move all their shared logic in there so I'm not maintaining duplicate code. I've tried this: class Recipe < Post; end class Article < Post; end class Post < ActiveRecord::Base #all the shared logic end All of these classes are in the standard ./app/models folder. This code, however, throws a ActiveRecord::StatementInvalid error when I go to /articles/new, for instance. The error is: Could not find table 'posts' Any idea how to set this up? Why

Why do cell renderers often extend JLabel?

余生长醉 提交于 2019-12-04 03:44:58
I noticed this is common. For example DefaultListCellRenderer, DefaultTableCellRenderer, and DefaultTreeCellRenderer all use it. Also a lot of the custom cell renderers I see online use this as well. I want to use a custom TableCellRenderer in my code, but I'm confused about whether I really need to subclass JLabel. What's the benefit of subclassing JLabel? camickr The API for the DefaultTableCellRenderer states: The table class defines a single cell renderer and uses it as a as a rubber-stamp for rendering all cells in the table; it renders the first cell, changes the contents of that cell