iOS document sequencing traverse folders by modify/create time
Source: Internet
Author: User
<span id="Label3"></p><p><p>Nsfilemanager in the Attributesofitematpath: method to get the file information in the sandbox, There are nsfilecreationdate and nsfilemodificationdate two attributes representing the file creation time and the modification time respectively, we can compare the two attributes of two files, and realize sorting by Time.<br>The two commonly used sorting methods are sortedarrayusingselector: and sortedarrayusingcomparator:, the latter method is used Here.<br>The code is as Follows.</p></p><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-built_in"><span class="hljs-built_in">Nsfilemanager</span></span>*filemgr = [<span class="hljs-built_in"><span class="hljs-built_in">Nsfilemanager</span></span>defaultmanager];<span class="hljs-built_in"><span class="hljs-built_in">Nsarray</span></span>*paths = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask,<span class="hljs-literal"><span class="hljs-literal">YES</span></span>);<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*rootpath = [paths objectatindex:<span class="hljs-number"><span class="hljs-number">0</span></span>];<span class="hljs-comment"><span class="hljs-comment">//get root directory</span></span> <span class="hljs-built_in"><span class="hljs-built_in">Nsarray</span></span>*paths = [filemgr subpathsatpath:rootpath];<span class="hljs-comment"><span class="hljs-comment">//get A list of files</span></span> <span class="hljs-built_in"><span class="hljs-built_in">Nsarray</span></span>*sortedpaths = [paths sortedarrayusingcomparator:^ (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>* firstpath,<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>* Secondpath) {<span class="hljs-comment"><span class="hljs-comment">//</span></span> <span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*firsturl = [rootpath stringbyappendingpathcomponent:firstpath];<span class="hljs-comment"><span class="hljs-comment">//get the full path of the previous file</span></span> <span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*secondurl = [rootpath stringbyappendingpathcomponent:secondpath];<span class="hljs-comment"><span class="hljs-comment">//get the full path of the latter file</span></span> <span class="hljs-built_in"><span class="hljs-built_in">nsdictionary</span></span>*firstfileinfo = [[<span class="hljs-built_in"><span class="hljs-built_in">Nsfilemanager</span></span>defaultmanager] Attributesofitematpath:firsturl error:<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>];<span class="hljs-comment"><span class="hljs-comment">//get Previous file information</span></span> <span class="hljs-built_in"><span class="hljs-built_in">nsdictionary</span></span>*secondfileinfo = [[<span class="hljs-built_in"><span class="hljs-built_in">Nsfilemanager</span></span>defaultmanager] Attributesofitematpath:secondurl error:<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>];<span class="hljs-comment"><span class="hljs-comment">//get The latter file information</span></span> <span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>Firstdata = [firstfileinfo objectforkey:nsfilemodificationdate];<span class="hljs-comment"><span class="hljs-comment">//get Previous file modification time</span></span> <span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>Seconddata = [secondfileinfo objectforkey:nsfilemodificationdate];<span class="hljs-comment"><span class="hljs-comment">//get A file modification time after</span></span> <span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[firstdata compare:seconddata];<span class="hljs-comment"><span class="hljs-comment">//ascending</span></span> <span class="hljs-comment"><span class="hljs-comment">//return [seconddata compare:firstdata];//descending</span></span>}];</code></pre><p><p>The resulting sortedpaths is the list of sorted files.</p></p> <p><p>iOS document sequencing traverse folders by modify/create time</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service