Objective-C 鍐呭瓨绠$悊娴呮瀽涓庡惊鐜己寮曠敤涓句緥

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Objective-C 鍐呭瓨绠$悊娴呮瀽涓庡惊鐜己寮曠敤涓句緥相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

鐞嗚В

  1. 鍙橀噺
  2. 浣滅敤鍩?/li>
  3. 鍙橀噺搴熷純
  4. 鎸佹湁瀵硅薄
  5. 閲婃斁瀵硅薄
  6. 瀵硅薄鎵€鏈夎€咃紙寮曠敤璁℃暟锛?/li>
  7. 瀵硅薄搴熷純
  8. 鍙橀噺鎵€鏈夋潈淇グ绗﹀瀵硅薄鐨勫奖鍝?/li>

瀵硅薄鐩镐簰鎸佹湁瀵艰嚧寰幆寮哄紩鐢紝涓句緥濡傚弽澶嶆墽琛屼换鍔$殑NSTimer瀵硅薄鐨勭洰鏍囧璞?br /> 鍙堜繚鐣欎簡璁℃椂鍣ㄥ璞★紝鑻ヨNSTimer瀵硅薄琚洰鏍囧璞℃寔鏈夛紝渚垮繀瀹氫細鍙戠敓寰幆寮哄紩鐢紝鍥犱负NSTimer瀵硅薄浼氭寔鏈夌洰鏍囷紝
鑰岃NSTimer瀵硅薄鍙堟槸鐩爣瀵硅薄鐨勬垚鍛樺彉閲忎篃灏辨槸鐩爣瀵硅薄鍙堟寔鏈夎NSTimer瀵硅薄
姝ゅ惊鐜己寮曠敤浼氫竴鐩存寔缁埌鐩爣瀵硅薄璋冪敤NSTimer瀵硅薄鐨刬nvalidate鏂规硶閲婃斁璇STimer瀵硅薄
鍙€氳繃鎵╁睍NSTimer绫诲埄鐢╞lock涓巁_weak鎵€鏈夋潈淇グ绗﹁В鍐宠寰幆寮哄紩鐢?/p>

#import<Foundation/Foundation.h>

@interface NSTimer(BlocksSupport)

+(NSTimer *)scheduledTimerWithTimeInterval:
(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats;

@end


@implementation NSTimer(BlockSupport)

+(NSTimer *)scheduledTimerWithTimeInterval:
(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats
{
return [self scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(blockInvoke:)
userInfo:[block copy]
repeats:repeats];

}

+(void)blockInvoke:(NSTimer *)timer{
void(^block)()=timer.userInfo;
if(block){
block();
}
}
//鍦ㄥ埆鐨凜lass绫讳娇鐢ㄧ殑鏃跺€?/span>
-(void)startAction{
__weak Class *weakSelf=self;//瀹氫箟涓€涓寚鍚戣嚜宸辩殑寮卞紩鐢?/span>
_myTimer=[NSTimer scheduledTimerWithInterval:5.0 block:^{
Class *strongSelf=weakSelf;/*鍧楁崟鑾疯鎸囧悜鑷繁鐨勫急寮曠敤锛屼笉鐩存帴鎹曡幏鏅€氱殑self鍙橀噺锛屽洜姝lock涓嶆寔鏈夎Class瀵硅薄
鐒跺悗椹笂璁_strong鍙橀噺鎸佹湁璇_weak鐨凜lass瀵硅薄淇濊瘉瀹炰緥鍦ㄦ墽琛屾湡闂存寔缁瓨娲伙紝
涔熷氨鏄strongSelf涔熸槸鎸佹湁浜咰lass瀵硅薄鐨勶紝浣嗘槸锛屼絾鏄紝浣嗘槸锛宻trongSelf鏄湪block浣?
涓0鏄庣殑鍙橀噺锛屽叾鐢熷懡鍛ㄦ湡浠呭瓨block鍧楋紝鍦╞lock鍧楁墽琛屽畬鍚庝究閲婃斁锛屼篃灏变笉鎸佹湁Class瀵硅薄浜?/span>*/
[strongSelf excueAction];    
} repeats:YES];
}

-dealloc{
[_myTimer invalidate];/*褰撳鐣屾寔鏈塁lass瀵硅薄鐨勬渶鍚庝竴涓紩鐢ㄥ皢鍏堕噴鏀剧殑鏃跺€欙紝璇ユ柟娉曚細琚墽琛岋紝浠庤€岃繕浼氳璁℃椂鍣ㄥ仠姝㈠伐浣溿€?
鑻ュ紑鍙戣€呭繕璁板湪dealloc涓皟鐢╥nvalidate鏂规硶鍋滄璁℃椂鍣紝閭d箞璁℃椂鍣ㄦ墽琛岀殑block鍧椾腑鐨剋eakSelf浼氬彉鎴恘il
strongSelf涔熷氨鎸囧悜浜唍il锛屼粠鑰岃鏃跺櫒鎵ц绌烘搷浣溿€?/span>*/
}

 

以上是关于Objective-C 鍐呭瓨绠$悊娴呮瀽涓庡惊鐜己寮曠敤涓句緥的主要内容,如果未能解决你的问题,请参考以下文章

鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽

Golang涔?sort 鎺掑簭婧愮爜娴呮瀽

520蹇箰~ 娴呮瀽 Vue.js 涓偅浜涚┖闂存崲鏃堕棿鐨勬搷浣滐紙濂芥枃鏀惰棌锛侊級

鍖哄垎 JVM 鍐呭瓨缁撴瀯銆?Java 鍐呭瓨妯″瀷 浠ュ強 Java 瀵硅薄妯″瀷 涓変釜姒傚康

[STL] 绠€鍗曟竻闄ap瀵硅薄鎵€鍗犵敤鍐呭瓨