zsh自动填充插件zsh-autosuggestionszsh

brew安装

brew install zsh-autosuggestions

Logs:

Last login: Wed Mar  6 10:07:19 on ttys000
➜  ~ brew install zsh-autosuggestions
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##O=-#     #
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##O=-#     #
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-autosuggestions/manifests/0.7.0-1
############################################################################################################################################################################## 100.0%
==> Fetching zsh-autosuggestions
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-autosuggestions/blobs/sha256:4537653cbf3540a0785481966b36291255839f1be4012a22409ef97c46a1860b
############################################################################################################################################################################## 100.0%
==> Pouring zsh-autosuggestions--0.7.0.all.bottle.1.tar.gz
==> Caveats
To activate the autosuggestions, add the following at the end of your .zshrc:

  source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

You will also need to restart your terminal for this change to take effect.
==> Summary
🍺  /opt/homebrew/Cellar/zsh-autosuggestions/0.7.0: 6 files, 44.8KB
==> Running `brew cleanup zsh-autosuggestions`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

根据上述提示,编辑~/.zshrc:在末尾追加

source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

最后,使之生效

source ~/.zshrc

效果:

zsh高亮插件zsh-syntax-highlighting

brew安装

brew install zsh-syntax-highlighting

logs:

Last login: Tue Mar  5 15:41:48 on ttys000
➜  ~ brew install zsh-syntax-highlighting
==> Downloading https://formulae.brew.sh/api/formula.jws.json
############################################################################################################################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-syntax-highlighting/manifests/0.8.0
############################################################################################################################################################################## 100.0%
==> Fetching zsh-syntax-highlighting
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-syntax-highlighting/blobs/sha256:603dabae4003cd3d95ab7f872a7fd9944e67cf0d963ffe42a07c8f3c191211ea
############################################################################################################################################################################## 100.0%
==> Pouring zsh-syntax-highlighting--0.8.0.arm64_sonoma.bottle.tar.gz
==> Caveats
To activate the syntax highlighting, add the following at the end of your .zshrc:
  source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

If you receive "highlighters directory not found" error message,
you may need to add the following to your .zshenv:
  export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR=/opt/homebrew/share/zsh-syntax-highlighting/highlighters
==> Summary
🍺  /opt/homebrew/Cellar/zsh-syntax-highlighting/0.8.0: 27 files, 203.4KB
==> Running `brew cleanup zsh-syntax-highlighting`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
➜  ~ 

根据上述提示,编辑~/.zshrc:在末尾追加

source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

最后,使之生效

source ~/.zshrc

效果:

使用之前
使用之前

使用之后
使用之后

Mac配置CocoaPods

本文介绍安装并使用CocoaPods

CocosPods的官网: https://www.cocoapods.org/

前言

CocoaPods是一个ruby gem,不知道具体是啥暂时也没关系~

安装CocoaPods的完整流程是:1、安装Homebrew -> 2、安装Ruby –> 3、安装并配置CocoaPods

三者之间有一个链式的依赖关系:Homebrew 用于安装 Ruby,然后 Ruby 用于运行 CocoaPods。

所以开始之前,我们先要了解两个东西,分别是Homebrew和Ruby。

1、Homebrew

官网:https://brew.sh/

Homebrew 是 Mac 操作系统上的一个包管理器,它可以让我们更方便地安装、更新和删除软件。Homebrew 可以用来安装 Ruby,也可以安装许多其他类型的软件。

安装Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Logs:

Last login: Tue Mar  5 10:23:16 on ttys000
➜  ~ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R rayvision:admin /opt/homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 1215, done.
remote: Counting objects: 100% (886/886), done.
remote: Compressing objects: 100% (270/270), done.
remote: Total 1215 (delta 648), reused 819 (delta 605), pack-reused 329
Receiving objects: 100% (1215/1215), 1.01 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (682/682), completed with 140 local objects.
From https://github.com/Homebrew/brew
 * [new branch]          build-cask-internal-json-v3 -> origin/build-cask-internal-json-v3
 * [new branch]          dependabot/bundler/Library/Homebrew/json_schemer-2.2.0 -> origin/dependabot/bundler/Library/Homebrew/json_schemer-2.2.0
   96f861e94..cf7c093b3  master     -> origin/master
 * [new branch]          memoize-installed-tap-loading -> origin/memoize-installed-tap-loading
 * [new branch]          sponsors-maintainers-man-completions -> origin/sponsors-maintainers-man-completions
 * [new tag]             4.2.10     -> 4.2.10
 * [new tag]             4.2.11     -> 4.2.11
HEAD is now at cf7c093b3 Merge pull request #16812 from cho-m/formula_auditor-also-check-tag
Updated 3 taps (homebrew/services, homebrew/core and homebrew/cask).
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

if there is a Warning like:

 Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.

Steps

Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

we should follow the steps with two command:

1、(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile

2、eval "$(/opt/homebrew/bin/brew shellenv)"

2、Ruby

官网:https://www.ruby-lang.org/en/

Ruby 是一种开源的编程语言,它易于理解和编写。CocoaPods 是用 Ruby 编写的一个gem,因此我们需要在计算机上安装 Ruby 才能使用 CocoaPods。

Mac系统自带的 Ruby 版本通常位于/usr/bin/ruby路径下,但自带的通常是较低版本,不建议用它来开发。建议同时安装最新版本的Ruby,并制定使用我们所安装的版本(当前最新版本为3.3.0)的 Ruby 版本进行开发。

我们可以采用rvm来管理多个不同版本的Ruby,就像flutter也可以使用fvm来管理。

查看默认的ruby:

/usr/bin/ruby -v

Logs:

ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

使用homebrew安装ruby

brew install ruby

Logs:

Last login: Tue Mar  5 11:05:10 on ttys001
➜  ~ brew install ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/manifests/3.3.0
############################################################################################################################################################################## 100.0%
==> Fetching dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
############################################################################################################################################################################## 100.0%
==> Fetching libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/blobs/sha256:98c0cf81bcdf7577d5fdc8cc18732970b9ae7e0e7423a733f88f0f566ba483ad
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
############################################################################################################################################################################## 100.0%
==> Fetching ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:5c99ffd0861f01adc19cab495027024f7d890e42a9e7b689706b85c8e2b9c9b3
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
############################################################################################################################################################################## 100.0%
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:fce7bf159988bf2b0960fa3d33ea810688d422c433b6461c7020cd0c937827a6
############################################################################################################################################################################## 100.0%
==> Fetching ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/blobs/sha256:f1df6988bf95afc9fd2f0f3c81f5ebbc44c329e0256e0cb9e6d1ff0afa87042c
############################################################################################################################################################################## 100.0%
==> Installing dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Installing ruby dependency: libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/5f6b0af1730aa1bc02f8021c90ed7ffe988eeba95eec83e6c828f77332ba6406--libyaml-0.2.5.bottle_manifest.json
==> Pouring libyaml--0.2.5.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 352.3KB
==> Installing ruby dependency: ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/13aa86e429e05d02a76005d1881eaf625091a5ac4dc7d7674c706d12ba48796a--ca-certificates-2023-12-12.bottle_manifest.json
==> Pouring ca-certificates--2023-12-12.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2023-12-12: 3 files, 226.7KB
==> Installing ruby dependency: openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/f7b6e249843882452d784a8cbc4e19231186230b9e485a2a284d5c1952a95ec2--openssl@3-3.2.1-1.bottle_manifest.json
==> Pouring openssl@3--3.2.1.arm64_sonoma.bottle.1.tar.gz
🍺  /opt/homebrew/Cellar/openssl@3/3.2.1: 6,874 files, 32MB
==> Installing ruby
==> Pouring ruby--3.3.0.arm64_sonoma.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
==> Summary
🍺  /opt/homebrew/Cellar/ruby/3.3.0: 19,656 files, 51.4MB
==> Running `brew cleanup ruby`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> ruby
By default, binaries installed by gem will be placed into:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
➜  ~

⚠️注意:要将当前Ruby设置为默认版本,不然在下一步安装CocoaPods会报兼容性错误,因为默认ruby版本太低了

默认版本太低报错

ERROR:  Error installing cocoapods:
	The last version of drb (>= 0) to support your Ruby & RubyGems was 2.0.6. Try installing it with `gem install drb -v 2.0.6` and then running the current command again
	drb requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

配置ruby到$PATH环境变量,默认使用当前最新版

echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"' >> ~/.zshrc
source ~/.zshrc

查看当前使用的Ruby版本

login: Tue Mar  5 11:18:48 on ttys002
➜  ~ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]

🏅没错,当前已经使用我们所安装的最新Ruby,版本是3.3.0。

接下来安装CocoaPods

3、CocoaPods

官网:https://www.cocoapods.org/

安装CocoaPods

sudo gem install cocoapods

Logs:

~ sudo gem install cocoapods
Password:
Fetching nanaimo-0.3.0.gem
Fetching colored2-3.1.2.gem
Fetching claide-1.1.0.gem
Fetching CFPropertyList-3.0.7.gem
Fetching atomos-0.1.3.gem
Fetching xcodeproj-1.24.0.gem
Fetching ruby-macho-2.5.1.gem
Fetching nap-1.1.0.gem
Fetching molinillo-0.8.0.gem
Fetching gh_inspector-1.1.3.gem
Fetching fourflusher-2.3.1.gem
Fetching escape-0.0.4.gem
Fetching cocoapods-try-1.2.0.gem
Fetching netrc-0.11.0.gem
Fetching cocoapods-trunk-1.6.0.gem
Fetching cocoapods-search-1.0.1.gem
Fetching cocoapods-plugins-1.0.0.gem
Fetching cocoapods-downloader-2.1.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.16.3.gem
Fetching ethon-0.16.0.gem
Fetching typhoeus-1.4.1.gem
Fetching public_suffix-4.0.7.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.2.3.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.6.gem
Fetching tzinfo-2.0.6.gem
Fetching i18n-1.14.1.gem
Fetching connection_pool-2.4.1.gem
Fetching activesupport-7.1.3.2.gem
Fetching cocoapods-1.15.2.gem
Fetching cocoapods-core-1.15.2.gem
Successfully installed nanaimo-0.3.0
Successfully installed colored2-3.1.2
Successfully installed claide-1.1.0
Successfully installed CFPropertyList-3.0.7
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.24.0
Successfully installed ruby-macho-2.5.1
Successfully installed nap-1.1.0
Successfully installed molinillo-0.8.0
Successfully installed gh_inspector-1.1.3
Successfully installed fourflusher-2.3.1
Successfully installed escape-0.0.4
Successfully installed cocoapods-try-1.2.0
Successfully installed netrc-0.11.0
Successfully installed cocoapods-trunk-1.6.0
Successfully installed cocoapods-search-1.0.1
Successfully installed cocoapods-plugins-1.0.0
Successfully installed cocoapods-downloader-2.1
Successfully installed cocoapods-deintegrate-1.0.5
Building native extensions. This could take a while...
Successfully installed ffi-1.16.3
Successfully installed ethon-0.16.0
Successfully installed typhoeus-1.4.1
Successfully installed public_suffix-4.0.7
Successfully installed fuzzy_match-2.0.4
Successfully installed concurrent-ruby-1.2.3
Successfully installed httpclient-2.8.3
A new major version is available for Algolia! Please now use the https://rubygems.org/gems/algolia gem to get the latest features.
Successfully installed algoliasearch-1.27.5
Successfully installed addressable-2.8.6
Successfully installed tzinfo-2.0.6
Successfully installed i18n-1.14.1
Successfully installed connection_pool-2.4.1
Successfully installed activesupport-7.1.3.2
Successfully installed cocoapods-core-1.15.2
Successfully installed cocoapods-1.15.2
Parsing documentation for nanaimo-0.3.0
Installing ri documentation for nanaimo-0.3.0
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for claide-1.1.0
Installing ri documentation for claide-1.1.0
Parsing documentation for CFPropertyList-3.0.7
Installing ri documentation for CFPropertyList-3.0.7
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for xcodeproj-1.24.0
Installing ri documentation for xcodeproj-1.24.0
Parsing documentation for ruby-macho-2.5.1
Installing ri documentation for ruby-macho-2.5.1
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.8.0
Installing ri documentation for molinillo-0.8.0
Parsing documentation for gh_inspector-1.1.3
Installing ri documentation for gh_inspector-1.1.3
Parsing documentation for fourflusher-2.3.1
Installing ri documentation for fourflusher-2.3.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.2.0
Installing ri documentation for cocoapods-try-1.2.0
Parsing documentation for netrc-0.11.0
Installing ri documentation for netrc-0.11.0
Parsing documentation for cocoapods-trunk-1.6.0
Installing ri documentation for cocoapods-trunk-1.6.0
Parsing documentation for cocoapods-search-1.0.1
Installing ri documentation for cocoapods-search-1.0.1
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-2.1
Installing ri documentation for cocoapods-downloader-2.1
Parsing documentation for cocoapods-deintegrate-1.0.5
Installing ri documentation for cocoapods-deintegrate-1.0.5
Parsing documentation for ffi-1.16.3
Installing ri documentation for ffi-1.16.3
Parsing documentation for ethon-0.16.0
Installing ri documentation for ethon-0.16.0
Parsing documentation for typhoeus-1.4.1
Installing ri documentation for typhoeus-1.4.1
Parsing documentation for public_suffix-4.0.7
Installing ri documentation for public_suffix-4.0.7
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for concurrent-ruby-1.2.3
Installing ri documentation for concurrent-ruby-1.2.3
Parsing documentation for httpclient-2.8.3
Installing ri documentation for httpclient-2.8.3
Parsing documentation for algoliasearch-1.27.5
Installing ri documentation for algoliasearch-1.27.5
Parsing documentation for addressable-2.8.6
Installing ri documentation for addressable-2.8.6
Parsing documentation for tzinfo-2.0.6
Installing ri documentation for tzinfo-2.0.6
Parsing documentation for i18n-1.14.1
Installing ri documentation for i18n-1.14.1
Parsing documentation for connection_pool-2.4.1
Installing ri documentation for connection_pool-2.4.1
Parsing documentation for activesupport-7.1.3.2
Couldn't find file to include 'activesupport/README.rdoc' from lib/active_support.rb
Installing ri documentation for activesupport-7.1.3.2
Parsing documentation for cocoapods-core-1.15.2
Installing ri documentation for cocoapods-core-1.15.2
Parsing documentation for cocoapods-1.15.2
Installing ri documentation for cocoapods-1.15.2
Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, tzinfo, i18n, connection_pool, activesupport, cocoapods-core, cocoapods after 10 seconds
34 gems installed

A new release of RubyGems is available: 3.5.4 → 3.5.6!
Run `gem update --system 3.5.6` to update your installation.

➜  ~

安装过程结束,接下来我们需要初始化pod,这个过程会设置master repo,也就是配置默认的源,但是这个过程也可能异常,稍后会介绍。

初始化pod

pod setup

🤯Oops,发生了一个意外:

zsh:command not found:pod

这个错误通常表示pod命令没有被正确添加到当前的$PATH环境变量中,也就是说,系统找不到pod命令的位置。

我们是通过gem install cocoapods命令安装的 CocoaPods,那么 pod 命令应该位于 Ruby Gems 的bin目录中。但这个目录可能没有被加入到$PATH环境变量中,因此需要手动添加。

依然以 zsh 为例,你需要做的是,打开~/.zshrc文件,然后添加下面这一行:

export PATH="$PATH:$(gem env gemdir)/bin"

或者使用命令行:

echo 'export PATH="$PATH:$(gem env gemdir)/bin"' >> ~/.zshrc
source ~/.zshrc

然后重新打开终端。

查验一下是否已经添加到环境变量中:

cat ~/.zshrc

export PATH="/user/local/bin:$PATH"
export PATH="/opt/homebrew/opt/ruby/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
export PATH="$PATH:$(gem env gemdir)/bin"

包含了export PATH="$PATH:$(gem env gemdir)/bin"就能使用pod指令了。

继续完成初始化工作

pod setup
Setup completed

使用Pod的搜索功能

pod search AFNetworking

搜索遇到问题:

Setup completed
[!]Unable to find a pod with name, author, summary, or description matching 'AFNetworking'

针对该问题,网络上一种

方案A:是认为是search缓存导致的,解决方案:

rm ~/Library/Caches/CocoaPods/search_index.json

实测无效🙅。另一种

方案B:是不管当前问题,直接使用Xcode创建一个iOS项目,并使用pod随便引用一个三方库如AFNetworking,最后执行pod install完成之后,再回到这里就能正常使用pod search功能。本文没尝试这种方案。。。

那该怎么解决呢?先看看pod环境

pod env

Logs:

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

解决方案是:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

其实这里补充很关键的一点是,首先,确保已经安装了 Xcode 和 Xcode 命令行工具。如果没有,请在 App Store 中下载并安装 Xcode,并打开终端输入以下命令以安装 Xcode 命令行工具:

xcode-select --install

最后我们再次检查pod环境:pod env,好了,环境已经没有问题了。

  ~ pod env

### Stack

```
   CocoaPods : 1.15.2
        Ruby : ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
    RubyGems : 3.5.4
        Host : macOS 14.3.1 (23D60)
       Xcode : 15.2 (15C500b)
         Git : git version 2.39.3 (Apple Git-145)
Ruby lib dir : /opt/homebrew/Cellar/ruby/3.3.0/lib
Repositories :
```

### Installation Source

```
Executable Path: /opt/homebrew/lib/ruby/gems/3.3.0/bin/pod
```

### Plugins

```
cocoapods-deintegrate : 1.0.5
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.1
cocoapods-trunk       : 1.6.0
cocoapods-try         : 1.2.0

到这一步,pod环境已经正常,但在此使用pod的搜索功能还是报错找不到对应的库。

我们查看一下本地已添加的私有仓库

pod repo

😧发现竟然一个都没有:

0 repos

这意味着没有本地仓库,那我们可以使用pod repo add命令添加一个私有仓库。例如,执行pod repo add <repo-name> <repo-url>来添加一个私有仓库。

这里添加官方私有仓库

pod repo add cocoapods https://github.com/CocoaPods/Specs.git

Logs

Cloning spec repo `cocoapods` from `https://github.com/CocoaPods/Specs.git`

这个过程需要等待一段时间⏳,指令结束后我们再次查看本地似有仓库,看到已经包含官方私有仓库了:

pod repo

cocoapods
- Type: git (remotes/origin/master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/rayvision/.cocoapods/repos/cocoapods

📌也有安装清华大学的私有仓库,但是本文主打一个能够科学上网、所以未采用该方案。

使用清华源:

pod repo add tuna https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git

[清华源]:https://mirrors.tuna.tsinghua.edu.cn/help/CocoaPods/

安装过程终于快收尾了,再来试试搜索功能:

pod search AFNetworking

没想到还是报一样的错误❌,找不到!!!

😊其实到这里重开一遍终端再次执行搜索指令,就能正常工作了。

或者在设置官方私有仓库之后执行:

pod repo update --verbose

Xcode 15.2 Pods build issue

使用Pod管理第三方库,在编译过程报错:

error: Sandbox: rsync.samba(8351) deny(1) file-write-create /Users/username/Library/Developer/Xcode/DerivedData/X-gslddcmdhniyplhiexxukkwkgtrz/Build/Products/Debug-iphoneos/X.app/Frameworks/AFNetworking.framework/.AFNetworking.s3QZ28 (in target 'X' from project 'X')
error: Sandbox: rsync.samba(8351) deny(1) file-write-create /Users/username/Library/Developer/Xcode/DerivedData/X-gslddcmdhniyplhiexxukkwkgtrz/Build/Products/Debug-iphoneos/X.app/Frameworks/AFNetworking.framework/.Info.plist.lzdXCz (in target 'X' from project 'X')

解决方案:

Build Settings找到ENABLE_USER_SCRIPT_SANDBOXING设置为NO

引用 https://forums.developer.apple.com/forums/thread/731041

《Head First Java》03_primitive主数据类型和引用

变量分为primitive主数据类型和引用

声明变量

  • 必须声明所有变量的类型
  • primitive主数据类型用来保存基本类型的值,包括整数、布尔和浮点数等
  • 对象引用保存的是对象的引用
  • variables must have a type, variables must have a name.
  • 类型在前,名称在后 int count;

primitive主数据类型(也叫基本数据类型)的大小

类型位数值域
boolean由JVM决定true / false
char16 bits0 ~ 65535
byte8 bits-128 ~ 127
short16 bits-32768 ~ 32767
int32 bits-2147483648 ~ 2147483647
long64 bits-很大 ~ +很大
float32 bits范围规模可变
double64 bits范围规模可变
基本数据类型 类型所占大小 及其取值范围

溢位:某个类型不足以装下某个具体值,会丢失精度

变量的命名规则

  • 能包含字母、数字、下划线
  • 但是字母不能在开头

关键字 key words

对象类型的引用

《Head First Java》02_类与对象

面向对象具有较强的适应性可拓展性

面向对象的继承Inheritance

  • 找出相同类的共同部分
  • 将共同部分提取成新的类
  • 将旧类以继承关系连接到新类
  • 子类可以覆盖父类的方法,来实现字类特有的方法
  • 父类比较抽象,字类比较具体

以对象来思考

  • 当设计一个类时,要记得对象是靠类的模型塑造出来的
  • 对象是已知的事物(实例变量instance variable)表示对象的状态,每个独立的对象都会拥有一份该类型的值
  • 对象会执行的动作(方法methods)表示对象的行为

创建对象

  • 编写类
  • 编写测试用的类
  • 在测试用的类中创建对象,并存取对象的变量、调用对象的方法

Java会拾荒(主动管理内存)

创建出来的Java对象会被存储在叫做堆(Heap)的内存中,这个堆最大的特点是支持回收垃圾(Garbage-Collectible Heap),若Java虚拟机察觉到某个对象将不会再被使用时,便会将其标记为“可回收”,当内存开始不足,垃圾收集器便会启动来清理垃圾回收空间,让空间能够再次被利用。

问与答

如何创建全局的变量或方法?

使用publicstaticfinal修饰

全局的函数与数据,是否与面向对象的概念相违背?

什么是Java程序,以及如何提交?

Java类由一组类组成,其中有一个类带有启动用的main()方法,用户需要安装Java虚拟机才能让程序运行。

若有成百上千个类时要如何提交?是否可以包装成类似单一应用程序的形式?

可将所有文件包装进依据pkzip格式来存档的Java Archive(.jar)文件,在该文件中可引入一个叫做manifest的文件、其内部定义jar的哪个文件带有启动程序的main()方法

要点

  • 面向对象设计拓展功能不需要改动之前已经测试好的程序代码
  • 所有的Java程序都定义在类中
  • 类如同蓝图、描述该类型的对象要如何创建
  • 对象自治,你无需在于它如何完成任务
  • 对象有已知的事物,并能执行工作
  • 对象本身已知道的事物称为实例变量,它代表对象的状态
  • 对象可执行的动作称为方法,它代表对象的行为
  • 创建类时,可能同时会需要创建独立、测试用的类
  • 类可以继承自较为抽象的父类
  • Java的程序在执行期是一组会互相交谈的对象

使用HandyJSON遇到的一个报错

根据HandyJSON官方仓库的使用介绍,在swift的枚举类型使用HandyJSON只需要让枚举遵守HandyJSONEnum协议即可。

To be convertable, An enum must conform to HandyJSONEnum protocol. Nothing special need to do now.

官方示例:

enum AnimalType: String, HandyJSONEnum {
    case Cat = "cat"
    case Dog = "dog"
    case Bird = "bird"
}

struct Animal: HandyJSON {
    var name: String?
    var type: AnimalType?
}

let jsonString = "{\"type\":\"cat\",\"name\":\"Tom\"}"
if let animal = Animal.deserialize(from: jsonString) {
    print(animal.type?.rawValue)
}

我的场景是,定义的枚举需要在OC中使用,所以用@objc public修饰了我的枚举,定义如下:

@objc public enum ConnectInteractionType: UInt, HandyJSONEnum {

    /// 未知
    case unknown = 0
    /// 触摸模式
    case touch = 1
    /// 指针模式(默认值)
    case point = 2

    func info() -> ConnectItemInfo {
        switch self {
        case .unknown:
            return ConnectItemInfo(title: "")
        case .touch:
            return ConnectItemInfo(title: "Touch Mode", normalIconName: "icon_interaction_touch")
        case .point:
            return ConnectItemInfo(title: "Pointer Mode", normalIconName: "icon_interaction_point")
        }
    }
}

错误信息:

Thread 1: "-[__SwiftValue unsignedLongLongValue]: unrecognized selector sent to instance 0x28126eca0"

经过排查,只有当我的枚举遵守了该协议,才会产生这个错误,所以确定错误与HandyJSONEnum是有关的。

而且,如果该枚举不使用混编,也就是取消掉@objc public后,也不会出现这个问题,

所以总结一下是:当swift中的枚举需要在oc中混编时,使用HandyJSON解析时,会解析失败。

《Head First Java》01_基本概念

Java的特点:

  • 友好的语法、
  • 面向对象、
  • 内存管理、
  • 跨平台可移植性。

目标:write once and run-anywhere.

工作方式:

  • 源代码(.java格式)
  • 编译器编译(javac)
  • 字节码(.class格式)
  • 在jvm上运行(可以将字节码转换成平台能够理解的形式来运行)

比如新建的一个java文件叫做MyFirstApp.java

使用javac编译器指令javac MyFirstApp会将其编译成MyFirstApp.class

最后使用%java MyFirstApp就能运行,但是得确保MyFirstApp.java内部有且仅有一个main函数

Java的版本:

  • 1.02到1.1都算Java
  • 1.2、1.3、和1.4都叫Java 2
  • 1.5开始叫做Java 5.0