如何在Objective-C中创建通用属性?

时间:2019-04-29 08:55:43

标签: objective-c

在Swift中,我们可以创建一个通用属性,例如:

class MyClass<T: MyOtherType where T: MyProtocol> {
    var property: T
}

在Objective-C中怎么可能?

2 个答案:

答案 0 :(得分:3)

假设:

#!/usr/bin/python
import os.path
import sys
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
# from PIL import Image, ImageTk
# from dbfunctions import *
import datetime

class GFPTime(tk.Tk):
    def __init__(self,*args,**kwargs):

        tk.Tk.__init__(self,*args,**kwargs)

        tk.Tk.wm_title(self,"GFP Employee Timecard System")
        tk.Tk.wm_geometry(self,"800x480+0+0")

        container =tk.Frame(self)
        container.pack(side='top',fill='both',expand= True)

        container.grid_rowconfigure(0,weight=1)
        container.grid_columnconfigure(0,weight=1)

        self.frames = {}

        for F in (MainMenu,RecordTime):

            frame = F(container,self)

            self.frames[F] = frame
            frame.grid(row=0,column=0,stick='nsew')

        self.show_frame(MainMenu)
        # print(self.frames.values())
    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()

    def empcbxlist(self, widget_name, criteria, output):
        # db = TimeCardDB()
        # cbvalue = db.listActiveEmployees()
        # db.dbclose()

        ob = self.frames.get( RecordTime ) 
        widget = getattr(ob,widget_name)
        widget[criteria] = output

###
class MainMenu(tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        #tk.Frame.configure(self,background='red')
        font9 = "-family {Minion Pro} -size 14 -weight bold"
        self.controller = controller

        recordBn = tk.Button(self,command=lambda: [self.controller.show_frame(RecordTime), self.controller.empcbxlist('dateLb','text','yessss')])
        recordBn.place(relx=0.269, rely=0.646, height=50, width=180)
        recordBn.configure(activebackground="#ececec")
        recordBn.configure(activeforeground="#000000")
        recordBn.configure(background="#d9d9d9")
        recordBn.configure(disabledforeground="#a3a3a3")
        recordBn.configure(font=font9)
        recordBn.configure(foreground="#000000")
        recordBn.configure(highlightbackground="#d9d9d9")
        recordBn.configure(highlightcolor="black")
        recordBn.configure(pady="0")
        recordBn.configure(text='''Record Time''')#!/usr/bin/python


class RecordTime(tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        self.dateLb = tk.Label(self)
        self.dateLb.place(relx=0.213, rely=0.021, height=38, width=144)
        self.dateLb.configure(activebackground="#f9f9f9")
        self.dateLb.configure(activeforeground="black")
        self.dateLb.configure(disabledforeground="#a3a3a3")
        self.dateLb.configure(font="-family {Minion Pro} -size 14 -weight bold")
        self.dateLb.configure(foreground="#000000")
        self.dateLb.configure(highlightbackground="#d9d9d9")
        self.dateLb.configure(highlightcolor="black")
        self.dateLb.configure(text='''SHOW DATE''')



GFPTime().mainloop()

您可以这样做:

@interface MyOtherType : NSObject

// Some code 

@end

@protocol MyProtocol <NSObject>

// Some code

@end

语法为@interface MyClass : NSObject @property MyOtherType <MyProtocol> * property; @end

实际上是类似于Class <Protocol>中的Class & Protocol类型。

答案 1 :(得分:0)

这是答案。

@interface myParentView< T: parentModel*> :UIView
 @property T myObject; // myObject is object of parentModel
@end

在所有子类中:

@interface myChildViewOne :myParentView<childModel>
// Now myObject is object of childModel
@end

Obj C具有复杂的语法,但是我们可以实现上述通用属性。