Objective C實現類似C++, Java中的範型
Objective C Generics
Objective C Generics是github上一個仿C++, Java, C#中範型的Objective C的實現.
用法如下:
將 ObjectiveCGenerics.h
拖進你的專案.在定義一個新類的時候,使用 GENERICSABLE
巨集宣告一下.
#import "ObjectiveCGenerics.h" GENERICSABLE(MyClass) @interface MyClass : NSObject<MyClass> @property (nonatomic, strong) NSString* name; @end
ObjectiveCGenerics.h
原始碼如下:
//Copyright 2013 Tomer Shiri [email protected] // //Licensed under the Apache License, Version 2.0 (the "License"); //you may not use this file except in compliance with the License. //You may obtain a copy of the License at // //http://www.apache.org/licenses/LICENSE-2.0 // //Unless required by applicable law or agreed to in writing, software //distributed under the License is distributed on an "AS IS" BASIS, //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //See the License for the specific language governing permissions and //limitations under the License. #if NS_BLOCKS_AVAILABLE #define GENERICSABLE(__className) \ GENERICSABLEWITHOUTBLOCKS(__className) \ GENERICSABLEWITHBLOCKS(__className) #else #define GENERICSABLE(__className) GENERICSABLEWITHOUTBLOCKS(__className) #endif #define GENERICSABLEWITHOUTBLOCKS(__className)\ @protocol __className <NSObject> \ @end \ @class __className; \ typedef NSComparisonResult (^__className##Comparator)(__className* obj1, __className* obj2); \ \ @interface NSEnumerator (__className##_NSEnumerator_Generics) <__className> \ - (__className*)nextObject; \ - (NSArray<__className>*)allObjects; \ @end \ \ @interface NSArray (__className##_NSArray_Generics) <__className> \ \ - (__className*)objectAtIndex:(NSUInteger)index; \ - (NSArray<__className>*)arrayByAddingObject:(__className*)anObject; \ - (NSArray*)arrayByAddingObjectsFromArray:(NSArray<__className>*)otherArray; \ - (BOOL)containsObject:(__className*)anObject; \ - (__className*)firstObjectCommonWithArray:(NSArray<__className>*)otherArray; \ - (NSUInteger)indexOfObject:(__className*)anObject; \ - (NSUInteger)indexOfObject:(__className*)anObject inRange:(NSRange)range; \ - (NSUInteger)indexOfObjectIdenticalTo:(__className*)anObject; \ - (NSUInteger)indexOfObjectIdenticalTo:(__className*)anObject inRange:(NSRange)range; \ - (BOOL)isEqualToArray:(NSArray<__className>*)otherArray; \ - (__className*)lastObject; \ - (NSEnumerator<__className>*)objectEnumerator; \ - (NSEnumerator<__className>*)reverseObjectEnumerator; \ - (NSArray<__className>*)sortedArrayUsingFunction:(NSInteger (*)(__className*, __className*, void *))comparator context:(void *)context; \ - (NSArray<__className>*)sortedArrayUsingFunction:(NSInteger (*)(__className*, __className*, void *))comparator context:(void *)context hint:(NSData *)hint; \ - (NSArray<__className>*)sortedArrayUsingSelector:(SEL)comparator; \ - (NSArray<__className>*)subarrayWithRange:(NSRange)range; \ - (NSArray<__className>*)objectsAtIndexes:(NSIndexSet *)indexes; \ - (__className*)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); \ \ + (NSArray<__className>*)array; \ + (NSArray<__className>*)arrayWithObject:(__className*)anObject; \ + (NSArray<__className>*)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt; \ + (NSArray<__className>*)arrayWithObjects:(__className*)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ + (NSArray<__className>*)arrayWithArray:(NSArray<__className>*)array; \ \ - (NSArray<__className>*)initWithObjects:(const id [])objects count:(NSUInteger)cnt; \ - (NSArray<__className>*)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ - (NSArray<__className>*)initWithArray:(NSArray *)array; \ - (NSArray<__className>*)initWithArray:(NSArray *)array copyItems:(BOOL)flag; \ \ + (NSArray<__className>*)arrayWithContentsOfFile:(NSString *)path; \ + (NSArray<__className>*)arrayWithContentsOfURL:(NSURL *)url; \ - (NSArray<__className>*)initWithContentsOfFile:(NSString *)path; \ - (NSArray<__className>*)initWithContentsOfURL:(NSURL *)url; \ \ @end \ \ @interface NSMutableArray (__className##_NSMutableArray_Generics) <__className> \ \ - (void)addObjectsFromArray:(NSArray<__className>*)otherArray; \ - (void)removeObject:(__className*)anObject inRange:(NSRange)range; \ - (void)removeObject:(__className*)anObject; \ - (void)removeObjectIdenticalTo:(__className*)anObject inRange:(NSRange)range; \ - (void)removeObjectIdenticalTo:(__className*)anObject; \ - (void)removeObjectsInArray:(NSArray<__className>*)otherArray; \ \ - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<__className>*)otherArray range:(NSRange)otherRange; \ - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<__className>*)otherArray; \ - (void)setArray:(NSArray<__className>*)otherArray; \ - (void)sortUsingFunction:(NSInteger (*)(__className*, __className*, void *))compare context:(void *)context; \ \ - (void)insertObjects:(NSArray<__className>*)objects atIndexes:(NSIndexSet *)indexes; \ - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; \ - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray<__className>*)objects; \ \ - (void)setObject:(__className*)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); \ \ + (NSMutableArray<__className>*)array; \ + (NSMutableArray<__className>*)arrayWithObject:(__className*)anObject; \ + (NSMutableArray<__className>*)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt; \ + (NSMutableArray<__className>*)arrayWithObjects:(__className*)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ + (NSMutableArray<__className>*)arrayWithArray:(NSArray<__className>*)array; \ \ - (NSMutableArray<__className>*)initWithObjects:(const id [])objects count:(NSUInteger)cnt; \ - (NSMutableArray<__className>*)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ - (NSMutableArray<__className>*)initWithArray:(NSArray *)array; \ - (NSMutableArray<__className>*)initWithArray:(NSArray *)array copyItems:(BOOL)flag; \ \ + (NSMutableArray<__className>*)arrayWithContentsOfFile:(NSString *)path; \ + (NSMutableArray<__className>*)arrayWithContentsOfURL:(NSURL *)url; \ - (NSMutableArray<__className>*)initWithContentsOfFile:(NSString *)path; \ - (NSMutableArray<__className>*)initWithContentsOfURL:(NSURL *)url; \ \ @end \ \ @interface NSSet (__className##_NSSet_Generics) <__className> \ \ - (__className*)member:(__className*)object; \ - (NSEnumerator<__className>*)objectEnumerator; \ \ - (NSArray<__className>*)allObjects; \ - (__className*)anyObject; \ - (BOOL)containsObject:(__className*)anObject; \ - (BOOL)intersectsSet:(NSSet<__className>*)otherSet; \ - (BOOL)isEqualToSet:(NSSet<__className>*)otherSet; \ - (BOOL)isSubsetOfSet:(NSSet<__className>*)otherSet; \ \ - (NSSet<__className>*)setByAddingObject:(__className*)anObject NS_AVAILABLE(10_5, 2_0); \ - (NSSet<__className>*)setByAddingObjectsFromSet:(NSSet<__className>*)other NS_AVAILABLE(10_5, 2_0); \ - (NSSet<__className>*)setByAddingObjectsFromArray:(NSArray *)other NS_AVAILABLE(10_5, 2_0); \ \ + (NSSet<__className>*)set; \ + (NSSet<__className>*)setWithObject:(__className*)object; \ + (NSSet<__className>*)setWithObjects:(const id [])objects count:(NSUInteger)cnt; \ + (NSSet<__className>*)setWithObjects:(__className*)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ + (NSSet<__className>*)setWithSet:(NSSet<__className>*)set; \ + (NSSet<__className>*)setWithArray:(NSArray<__className>*)array; \ \ - (NSSet<__className>*)initWithObjects:(const id [])objects count:(NSUInteger)cnt; \ - (NSSet<__className>*)initWithObjects:(__className*)firstObj, ... NS_REQUIRES_NIL_TERMINATION; \ - (NSSet<__className>*)initWithSet:(NSSet<__className>*)set; \ - (NSSet<__className>*)initWithSet:(NSSet<__className>*)set copyItems:(BOOL)flag; \ - (NSSet<__className>*)initWithArray:(NSArray<__className>*)array; \ \ @end \ \ @interface NSMutableSet (__className##_NSMutableSet_Generics) <__className> \ \ - (void)addObject:(__className*)object; \ - (void)removeObject:(__className*)object; \ - (void)addObjectsFromArray:(NSArray<__className>*)array; \ - (void)intersectSet:(NSSet<__className>*)otherSet; \ - (void)minusSet:(NSSet<__className>*)otherSet; \ - (void)unionSet:(NSSet<__className>*)otherSet; \ \ - (void)setSet:(NSSet<__className>*)otherSet; \ + (NSSet<__className>*)setWithCapacity:(NSUInteger)numItems; \ - (NSSet<__className>*)initWithCapacity:(NSUInteger)numItems; \ \ @end \ \ @interface NSCountedSet (__className##_NSCountedSet_Generics) <__className> \ \ - (NSSet<__className>*)initWithCapacity:(NSUInteger)numItems; \ - (NSSet<__className>*)initWithArray:(NSArray<__className>*)array; \ - (NSSet<__className>*)initWithSet:(NSSet<__className>*)set; \ - (NSUInteger)countForObject:(__className*)object; \ - (NSEnumerator<__className>*)objectEnumerator; \ - (void)addObject:(__className*)object; \ - (void)removeObject:(__className*)object; \ \ @end \ #if NS_BLOCKS_AVAILABLE #define GENERICSABLEWITHBLOCKS(__className) \ \ @interface NSMutableArray (__className##_NSMutableArray_BLOCKS_Generics) <__className> \ - (void)sortUsingComparator:(__className##Comparator)cmptr NS_AVAILABLE(10_6, 4_0); \ - (void)sortWithOptions:(NSSortOptions)opts usingComparator:(__className##Comparator)cmptr NS_AVAILABLE(10_6, 4_0); \ @end \ @interface NSSet (__className##_NSSet_BLOCKS_Generics) <__className> \ - (void)enumerateObjectsUsingBlock:(void (^)(__className* obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); \ - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(__className* obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); \ - (NSSet<__className>*)objectsPassingTest:(BOOL (^)(__className* obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); \ - (NSSet<__className>*)objectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(__className* obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); \ @end \ #endif
相關推薦
Objective C實現類似C++, Java中的範型
Objective C Generics Objective C Generics是github上一個仿C++, Java, C#中範型的Objective C的實現. 用法如下: 將 ObjectiveCGenerics.h 拖進你的專案.在定義一個新類的時候,使用 GE
C#實現類似C++功能的困惑
我要實現一個功能,用C++表達如下: int g_iTest =0; // 全域性變數class A{A(int* pVal){m_pVal = pVal;} //將指標記錄下來void Increase(){(*m_pVal)++;}}//A a(&g_iTest);a.Increase();
SQL Server 實現類似C#中 PadLeft功能
left ret cnblogs alt div use sql eat pan 1 USE [Test] 2 GO 3 SET ANSI_NULLS ON 4 GO 5 SET QUOTED_IDENTIFIER ON 6 GO 7 --@column 表
C#實現獲取枚舉中元素個數的方法
ger ref splay ogre strong sharp lock scrip line 本文以一個簡單實例講述了C#實現獲取枚舉中元素個數的方法,對於C#學習來說是需要加以牢固掌握的技巧。分享給大家供大家參考之用。具體如下: 實現該功能可以使用枚舉基類System
C++實現分類統計字串中字元個數
#include <iostream> using namespace std; int main(int argc, char **argv) { int a,b,c,d,e,len;
C#實現類似百度網盤、360雲盤在“我的電腦”磁碟驅動器
我們在安裝360雲盤或百度網盤後,一般在我的電腦中會看到類似如下的介面: 類似這樣的快捷方式是如何實現的呢?下面為大家一一講解實現思路 1、這裡顯示出來的磁碟快捷方式,實際上在登錄檔中有相對應的記錄,開始--執行,輸入regedit,回車,會開啟登錄檔,請定位到:HKE
C++實現WebBrowser控制元件中對滑鼠點選事件的監聽,並獲取所點選標籤的超連結
主要步驟如下: 1、接收網頁事件(參考資料) 要響應網頁事件,需要實現IDispatch介面,並在其Invoke()方法中處理收到的訊息。對於MFC,因為CCmdTarget類已經實現了該介面,所以只需要繼承CCmdTarget並結合相關巨
c++中virtual關鍵字的作用與Java中多型的一點對比
轉載至:https://segmentfault.com/a/1190000006119157 動機 最近一直在使用C++寫win32程式,用了一些庫,裡面提供的類和demo各種是virtual這個關鍵字,一直不是很明白到底是啥用,於是查看了一些文件,寫小程式來實驗
各種內排序演算法的C++實現(不斷更新中)
和很多計算機系的同學們一樣,我在大學二年級時也學了《資料結構》這門課。當時我的老師是一箇中科大的博士,現在已經是教授了。他在課上曾經這樣評價這門課:《資料結構》幾乎是所有計算機課程的基礎課,如果把這門課學好了,其他的專業課就不成問題了。還有,IT公司的面試經常涉及
C#實現對SQL資料庫中的表的查詢、新增、修改、刪除資料
經常用到的物件有:SqlConnection,SqlAdapter,SqlCommand、Dataset、DataGrid和DataReader等,以SqlConnection,SqlAdapter,SqlCommand、Dataset、DataGrid物件,操作SQL的例
C++虛擬函式與JAVA中抽象函式比較 介面與抽象類比較
C++虛擬函式與Java中抽象函式比較 1:java中沒有虛擬函式的概念,但是有抽象函式的概念,用abstract關鍵字表示,java中抽象函式必須在抽象類中,而且抽象 函式不能有函式體,抽象類不能被例項化,只能由其子類實現抽象函式,如果某個抽象類的子類仍是抽象類,那麼該子
c#實現控制元件(ocx)中的事件
c#控制元件實現類似c++中ocx控制元件功能 c++中ocx控制元件 1、控制元件方法 2、控制元件事件 c#很容易實現c++中ocx中控制元件方法的功能,但是實現類似c++中ocx的控制元件事件,則需要一定的周折。 下面就用例項簡單的介紹c#如何實現 c#中Activ
【kotlin】基本語法when的使用,類似於java中的switch,但是又青出於藍而勝於藍
href .com log 事情 IT 使用 基本語法 kotlin 參數 when(要判斷的參數){ 參數值為1 ->做這種事情 參數值為2 ->做另一種事情 else -> 類似於switch中的default } 擴展使用:ht
C實現繼承、封裝和多型思路
1、繼承 把父類資料結構放在子資料結構的首位置,方便以後資料訪問和資料的強轉 struct parent{ int info; }; struct child { s
C#實現氣泡排序之對泛型排序
在前面的文章中我們講述了C#如何實現氣泡排序!那麼有沒有想過如何實現對任意的資料型別進行氣泡排序呢?這裡我們將就此問題進行解答!首先我們瞭解到氣泡排序的本質就是升序或者降序排列一個數組的元素!我們首先去舉個例子來感受一下氣泡排序,像整型陣列就是這樣排序: /
關於mybatis根據傳進來的引數執行不同的語句,類似於java中的if~else~語句
1、在一個方法中根據條件執行兩個不同的查詢語句,例如下面的查詢穿進來的引數為admin時執行第一個查詢語句,否則就執行下面查詢語句 dao類中的方法 public List<Caidan> getcaidan(@Param(value="czyid") Stri
Android中實現類似探探中圖片左右滑動切換效果
偶然之間發現探探的左右滑動的圖片挺好玩,試著去做了下,再到後來,看到許多大神也推出了同樣仿探探效果的部落格,從頭到尾閱讀下來,寫得通俗易懂,基本上沒什麼問題。於是,實現仿探探效果的想法再次出現在腦海中。那麼,還猶豫什麼,趁熱來一發吧!就這麼愉快地決定了。
C++實現呼叫C#(CLI)方法
把C#編譯成DLL或者Axtive控制元件,再由C呼叫 ! 比如使用C++呼叫C#的DLL SwfDotNet是C#編寫的,作者的C#水平,真是令我佩服。這是個特別好的讀寫Swf檔案的庫。但是,我要用在C++專案中,怎麼讓C++呼叫C#的DLL呢。今天一上午都在琢磨這個問
Java中泛型通配符的一點概念
for name 安全 msg simple clist getc 保持 spa 以List<T>為例,通常如果我們想在List中存儲某種類型的數據,我們會用下面的語法進行聲明和使用: 1 List<String> allMsg =
Java中泛型Class<T>、T與Class<?>
arraylist new 對象類型 創建 集合 spa div get 裏的 一.區別 單獨的T 代表一個類型 ,而 Class<T>代表這個類型所對應的類, Class<?>表示類型不確定的類 E - Element (在集合中使用,因為集合