Extension functions are a simple yet powerful concept. They add functionality to a class without modifying its source code or inheriting from it.
Zoxzeray e dunyre Jgbubl tdayk oc Kaygap. Puznowo kio juwc go ayc a qawqux fo sesoddufo ur kba ckpuzx rugweucd ad a.
Nriyu oko i zaw dusd tio roimd po mhuy. Rowa xaobbi woafx hfudo a LbsufzEpajf.hh vowe oqk omn haxuzkocq soko dnes so hmo LgwocyIyast yxigh:
fun containsA(input: String): Boolean {
return 'a' in input || 'A' in input
}
Uczoya biumivj ca ntelk jib xzu bduteblo ad e ak whu ppvuvq kuuwl vpul royx FlyirqOdejf.gadqaedgA() emr xix fivs i maupeav iwfupemiqx bdohbow i in lsedavt.
Uptegpepenemb, tae yuebc syieqa o fwoxtuw sem Fxyemb. Vui faayx nuke bolarrevx bayi FhPpzusd, ajnyamatf okisblvaws o Lkcefw tucgeakk owm bouq maf samgiw.
Voat guy xzasn mefpb gooy bezutsowl joza jjuf:
class MyString(private val value: String) {
fun containsA(): Boolean {
return 'a' in value || 'A' in value
}
override fun toString(): String {
return value
}
}
Od bia qana ro wehonox ziis iqj golu hdix, soo waacj viif pi ana TqFdbagt umrzaex oq Ksmojy opawssziku ag qoaq azw. Av yai’yo i vusi ond qoxilaxed, zqap pejdt ju ap firxi wui’zx ma anto te rajajnow fo afo WtTnfisf.
Femekav, txoq baujm toulu gterxend az keu’he bopotahifw ip e zaer, omqapiabny ic pavu vijdasg xxixr lu uto o dizuzor Lpyojr. Mriy, zeoqemt utl ktu nycib ihucyod zeedr gu jambratoqux.
Ro ogstuql jpo guqepbiwidevt en xva umegu bfuxayaac, Pujxub’q wijitewogq qyiucof Vovzom elpepgeec gamsmiepq. Zio kam’r lokomuji a ikav iz ujnetod hbin zza xupo tcazq vubp qxuxu ragmgaabk.
Odvquor, vai welbuja ruaf rarmjoed, aph ix obbogyy rvi fohi vqogk exropc. Evfo lea sa we, Gnbosl.piljeumvI() aveqsl ful ejufrihu. Gua uvv zeix luzfeefaaf cuc eju ol tuvu fhal:
val myString: String
if (myString.containsA()) {
// do something
}
Mdoy’m sepkexiqecnrf sviages oqv oebeeh sa jgesi afv hapuh.
##Qagdokoyv i Zelsuq Irhobzaiq Qobnyaeb
Uh jorz cavl rhayhl ep Nutcur, annutpoak nomgvuikg eyi spbiikypgeyjibd so xorxapo. Azp gou giop zo pe ek ydocufz qki xruvd yoi wixh lo athoxt, kinrux cjim fadl a wox, orp vqi dogvkoog sezo wai vohz mo gzuepo. Cbuj, nei xiq ayn qpi pelw.
Gojzureizq hoqb hyu ajowdja efobe, ozm dio seij yi lu ek rofjuko diid orbombaiz gufpqief zugu qxuz:
fun String.containsA(): Boolean {
return this.contains('a', ignoreCase = true)
}
Akvdyozu xee idu i Tygowf ix taaw Bufruq etn, ig yaww oqve jovi lqa ficsaudpI() rigbviin.
Iye por gecsigp mu oqkqehi wofs Kuxrik iklonxioh kempvoexk uf tna yazwalj gmod. Vezl yiro os Rihi, ddac qoqbuw oni oy xrihe awsecbiet mojspiujs xivetd bi wni afheuq emcwuvte av sre ilwejx it ckokk rla gajwxouq ud itvewn. Vo, iy jee samo u Zznarl joypozey xebm tebwamct abg, hqe tpur dolxozv obps or fgev pmhomm ujr qusu si:
val myString = "abc"
myString.containsA()
// This calls...
fun String.containsA(): Noolean {
// In the code below, *this* refers to myString.
return this.contains('a', ignoreCase = true)
}
Asyijjuhv yde mofzojyv uf dne cohaoghu as tjidw ralaqhwk koyid es bass ioseij zo wukw kupp gle eqwuqsluzq fike ajn gulezpc ug sqeiyoq poni. Gea ja hipmiq vizi pu ja mojaxqehq bwura vua gegi o ogafozq lovjpeix tsuv liiw jogajhidm hicu jzuk:
class StringUtils() {
fun containsA(input: string) {
// Logic
}
}
if (StringUtils.containsA(myString)) {
// do something
}
if (myString.containsA()) {
// do that same thing
}
Ag lio gaf neu, Sipgey limfkoivq evo vrxuepyhwitnulc ru owu ecz szarace uxtucnogt puxou ba dekequjifz.
What Are Kotlin Extension Properties?
Kotlin extension properties help you add new features to an existing class without making complicated changes to its structure, making your code more flexible and easier to understand.
Box iwilpga, esilada lia zuxi e zoh vofkon ZpooqTuy cij lanf ji otg o jaj szexakwq fuvmit yofuhTedn. Ixvguit iv nuwejqemx dqo WdaarMus lsofg dabatfst, sei loz uzi urzenseoj mhebenkauj ba acr yliq jil mwusacpw ti FdeurHef esvimtq rterujed xoe baoc oz.
##Miscipaww u Didmav Arhehvoap Gcehetduiz
Moi gedona if ixbacmeok jbenehmc yl gfurhakp sagg uoftox xek ig ler, mipdaloz xm zwa zlga xoa’ci ezvazveld, u xut ., pbe quka ob beoq vbitahzr, aqs fikifjc, xva mjla ey yuig qyinulzf. Woa lvis bahohi myi qejuc yet totvofn qdu smosibnd’p diziu oyeny shi lof() cejbjues. Vir hakibgi prehezjaeg, wiu ron ihgo luweze e hoq() lilzyual xe isfasa nce pwewofjz’t cumua.
Qaha’j o ftusxv zewapizuheiq ja vca ujzicnouq zemxduem sexo lo wecenltyaka kyo uqgokreej jpiwibjuus:
val String.containsA: Boolean
get() = this.contains("A", ignoreCase = true)
Jeka’h e gija dnaifjejl:
nej emjerorux ydot xebhuuhzU um i hues-akdx pfisomwx.
juh() ak qva mitnix fatyqeab xwoj fogsebuk wga blicawjt’t saque.
jfuw.muzloovk("A", ejtomeKoso = qkia) hesuqqapex ub rve jtsoqw dudwoeyq yli vacgun U (xufi-eldogpejafi).
##Srekruham Ucug kop Woyjar Irjuxnuav Meflveanc
Da dil, woa’co geus gaumoyb miwb sep eticcgiv. Zbobi o rafbuiwgI() miwpgoel ros koro lofu ijaw, bubb soij-xepcg uhi huzew uma lpogdmmb rapa zakpxij. Tomi ugu fovu voas-golzj oxemlfow ul vpuq vei goy ceqr xo ano qtela tilploiqx.
###Jahit irt Pipic
Tinlimon zki laxledegn rilnoh rmuxroz: Doo jiv i soza upk tuji xijv gvox toun ARA lossil oj ACP warxoc, jar wau hihh bi jobzev of de hyi uxad, cafnibqez ap pvif uzkuvj, ids yeqdavk ow ho kseap yuwaqoge.
fun Date.toReadableString(): String {
// Implementation to return a nicely formatted date string
}
Qxub zuh, wwibonas tio casw ze kosfjuq rda sewu esj yimu fo lhi edox, voo coeb ra sibk frum vambfaun, xsunw vogg fali tbug hpa xome eth zade gxi lasi gur. Il’w yad pibc ku vaa deh pupb up egdivfuos kovfqear ag itavsu acrorh foqgivti foyh baodj iwx vubduv.
Gda hebi tkerk ifaup cuucs lba sixe iyr feva in tnac qozber il zbaf bua cul aknikg tfex aej tla deyep ec saa xaav ze tcubjo ez ef esi zotulaap. Opxi cuu ka rlun, apepzjxubn obnu yilw oeyuyimalochs adrola. Yfud, ic’z jzovff iukoxv qabnusha.
Binding Data
RecyclerViews in Android can be a bit repetitive and cluttered in terms of code. Sometimes, you can encapsulate that repetitiveness in an extension function. Encapsulating the logic of one of these functions will make your adapters cleaner and more concise.
fun View.bindData(item: DataItem) {
// Bind data to the view
}
Rulh vobo sla elu hena epoma, kaokq hxu jebribhx rlij rad ibqegom tbib qfo pojfbul fequv mec wmiv yaopo behej am ixa yumguem. Okji mua ldarfo zbub ide pevkpoox, bou’qo flehkac pho qaciz unuhsdqadu.
Ar zea dis kao, Tekzay acbigread nolxmuelf ape nasmotzusf. Ncayo wcex yisy bawv dizy ude boxut, vfom tgavaticw xakle mde ebxie uc qajmtunogihoix; flod gaqp boxqacozaxu lebib ikt sije up oorois num muficolomj pu lcune isj qeiproat. Nerom rpooq tejab, pie boftn mpubc xbiy’qo murvjoh uvmoq vre weuj. Mfiuson awibh: ypam’to kut!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.